• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Specificities of string_pos (SOLVED)

Fixer90

Member
The function string_pos will return the position of a given substring within a given string. But let's say I have the following code:
Code:
text = "AAABAAABAAA";
Now, I want to run the string_pos function to find out the position of the 2nd B in this string, and only that 2nd B. What do I do?
 

Dmi7ry

Member
Code:
/// string_pos_ext(string, char, index)

var text = argument0;
var char = argument1;
var index = max(0, argument2);

for (var i=1; i<=string_length(text); i++)
{
    if string_char_at(text, i) == char
    {
        index--;
        if index < 1 return i;
    }
}

return -1;
usage:
Code:
var text = "AAABAAABAAA";

var pos = string_pos_ext(text, "B", 1);
show_message(pos);
 
Top