The function STRING_FIND looks for a particular substring within a string and outputs the location (by index number) of said substring.

This function takes two strings as input: one to search through, and one to designate what to look for. It outputs the index number that indicates where in the first string the second string first appears. If the second string does not appear at any point in the first string, this function outputs -1.

Declaration

STRING_FIND(string, search_string) -> index

 

Parameters

string (required, type: string)
The string to search through.
search_string (required, type: string)
The string to search for.

Return Values

index (type: number)
The index number indicating the location of the first character of the first instance of search_string to appear within string. Indexing begins at zero. An index value of -1 indicates no instances of search_string exist within string.

Examples

The following example finds the substring "string" within the larger string "Where is the string?". "string" occupies the fourteenth through nineteenth characters of "Where is the string?", so STRING_FIND outputs the number 13. Remember, indexing begins at zero, so 13 is the index number of the fourteenth character:
STRING_FIND("Where is the string?", "string") -> 13

If there are multiple instances of the search string within the base string, STRING_FIND outputs the location of the first. The following example searches for "strings" within the larger string "strings strings strings" and – because indexing begins at zero – outputs 0 to indicate that "strings" is the very first substring to appear within "strings strings strings":

STRING_FIND("strings strings strings", "strings") -> 0

The search executed by STRING_FIND is case sensitive; "Strings" with a capital S is not the same as the "strings" which is lowercase throughout. The following example takes input very nearly identical to the previous example, but the first character is capitalized, and so the first instance of "strings" starts not at the first character of "Strings strings strings: but at the ninth:

STRING_FIND("Strings strings strings", "strings") -> 8

The following example searches for the string "coconuts" within "This is a string". Nowhere in "This is a string" does the string "coconuts" appear, and so this function outputs -1:

STRING_FIND("This is a string", "coconuts") -> -1
A search string is required in order for the STRING_FIND function to run, but the string that makes up the search string does not technically need to contain any characters. In such cases, the output of STRING_FIND will always be 0. The following example searches for a blank string within "This is a string" and behaves accordingly:
STRING_FIND("This is a string", "") -> 0

For the sake of simplicity, the previous examples took hardcoded strings as input, but this does not need to be the case. Assume the last example has access to the following variables:

big_string = "This is a relatively big string"
little_string = "is"

The following example takes the contents of big_string and searches for the contents of little_string within them. The first appearance of "is" within "This is a relatively big string" begins at the third character – "is" is within "This" – and thus the index is 2:

STRING_FIND(big_string, little_string) -> 2

 

Was this article helpful?

0 out of 0 found this helpful

Add comment

Article is closed for comments.