How to find which substring comes first in array?

Godot Version

4.2.2

Question

Hello!
Lets say I have an array that changes the first number depending on what value is set:
[“2PAR”, “4MAY”, “5EN1”, “5EN2”, “6EN3”, “7LDG”]

How could I find when, for example, the next “PAR” and “MAY” comes if we are starting the array in index 5?
And then additionally, how could I find which one comes first?

1 Like
func find_first_sub_string_index(sub_string : String, array : Array[String], start_index : int):
   for i in range(start_index, array.size()):
      if array[i].contains(sub_string):
         return i
   
   return -1

If we are to assume that you are only checking for two strings then this function will return the first one:

func find_it(s1:String, s2:String, a:Array[String], n:int)->String:
	var b = a.slice(n).filter(func(i):
		if i.contains(s1) or i.contains(s2):
			return i
		)
	return b[0]

Where s1 and s2 are the two strings you are searching for, a is the array to be searched, and n is the offset.