GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2016-05-12 19:28:50

nmejames
Member
Registered: 2016-04-03
Posts: 2

string_find_all

Expand/// string_find_all(str, substr)
//  Returns an array containing the position of all instances of substr in str.

var s, ss, slen, sslen;
var ar, n;

s  = argument[0];
ss = argument[0];

slen  = string_length(s);
sslen = string_length(ss);

n = 0;
for (var = i = 1, j = 1; i <= slen; i++)
{
	if (string_char_at(ss, j) == string_char_at(s, i))
	{  j++;  }

	if (j == sslen)
	{
		ar[n] = (i - sslen)+1;
		n++;
		j = 0;
	}
}

return ar;

Last edited by nmejames (2018-01-25 03:08:51)

Offline

#2 2016-05-13 12:13:05

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

Re: string_find_all

This is a good idea for a script — sort of surprised it's not part of the collection already. Still considering whether or not "tootoot" should find "toot" once or twice. Obviously your way is the faster option. A simple regex query would only find it once so I guess this is good.

Thanks for the submission.


Abusing forum power since 1986.

Offline

#3 2016-05-20 07:15:14

gnysek
Member
Registered: 2011-12-29
Posts: 36

Re: string_find_all

As example:

Expand<?php
$str = 'tootoot';
var_dump(substr_count($str, 'toot'));

returns 1, so it's another vote for keeping it this way. But it would be good to have string_find_all_overlapped or something too.

Offline

Board footer

Powered by FluxBB