• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

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