Deprecated: Function get_magic_quotes_runtime() is deprecated in /home/public/forums/include/common.php on line 79

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/public/forums/include/common.php on line 83
string_get_between / Script Submission / GMLscripts.com

GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2021-05-08 09:19:17

maras
Member
Registered: 2021-04-25
Posts: 25
Website

string_get_between

might be useful
UPDATE: added a skip option
UPDATE: added a startfrom option because it's even more useful than the skip option

Expand/// 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#5104

Offline

#2 2021-05-25 14:19:17

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: string_get_between

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

#3 2021-05-26 18:30:04

maras
Member
Registered: 2021-04-25
Posts: 25
Website

Re: string_get_between

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#5104

Offline

Board footer

Powered by FluxBB