You are not logged in.
Pages: 1
might be useful
UPDATE: added a skip option
UPDATE: added a startfrom option because it's even more useful than the skip option
/// string_get_between(str, substr1, substr2, skip, startfrom)
//
// returns string
//
// gets a string between two substrings
//
// str string, string
// substr1 substring from, string
// substr2 substring to, string
// skip skip the first n substrings1 (optional argument, defualt 0), real
// startfrom start from the nth character (optional argument, default 1), real
//
// if no substr2 is found it returns everything from substr1 till the end
//
// var str = "...so it's located at [x=25.1, y=7.524] and...";
// string_get_between(str, "x=", ","); returns 25.1
// string_get_between(str, "y=", "]"); returns 7.524
//
// var str = "it's fee, it's faa, it's foo";
// string_get_between(str, "it's ", ","); returns fee
// string_get_between(str, "it's ", ",", 1); returns faa
// string_get_between(str, "it's ", ",", 2); returns foo
//
/// GMLscripts.com/license
function string_get_between(str, substr1, substr2, skip = 0, startfrom = 1) {
var first_found = false;
var str_between = "";
var times_found = 0;
var str_length = string_length(str);
var substr1_length = string_length(substr1);
var substr2_length = string_length(substr2);
for(var i = startfrom; i < str_length; i++) {
if !first_found {
if string_ord_at(str, i) == string_ord_at(substr1, 1) {
var same_chars = true;
for(var s = 0; s < substr1_length-1; s++) {
same_chars = string_ord_at(str, i+s) == string_ord_at(substr1, s+1);
if !same_chars break;
}
if same_chars {
if times_found++ == skip {
first_found = true;
i += substr1_length-1;
}
}
}
}
else {
if string_ord_at(str, i) == string_ord_at(substr2, 1) {
var same_chars = true;
for(var s = 0; s < substr2_length-1; s++) {
same_chars = string_ord_at(str, i+s) == string_ord_at(substr2, s+1);
if !same_chars break;
}
if same_chars return str_between;
}
str_between += string_char_at(str, i);
}
}
if first_found str_between += string_char_at(str, i);
return str_between;
}
Last edited by maras (2022-05-20 07:48:43)
I'm on the official GM discord > maras_cz
Offline
Hmmm. I have to think about this. I'm trying to imagine how I would use this in practice. Also, it seems like this could be simplified and sped up with `string_pos_ext()`.
Apologies for the delayed reply. I've been changing web browsers a lot lately and forget that I'm not always logged in here so I miss new messages.
Abusing forum power since 1986.
Offline
I feel like this is faster than using string_pos because this script checks the string and returns the result in one go
With string_pos you'd need to call it twice and then use string_copy or something to get the result
I'm on the official GM discord > maras_cz
Offline
Pages: 1