indexOf
Function: indexOf()
Returns the character position of a sub-string within a string.
Syntax
indexOf(stringToTest, stringToLookFor)
Argument | Type | Description |
---|---|---|
stringToTest | String | The string that will be searched. |
stringToLookFor | String | The sub-string to look for within the string. |
If the string does not exist, indexOf returns 0.
If there are multiple hits for the sub-string, indexOf returns the position of the first occurrence.Â
Examples
indexOf("Thomas Troubridge", " ")
Returns 7, the position of the first space in "Thomas Troubridge".
indexOf("Thomas Troubridge", "bridge")
Returns 12, the start of the sub-string "bridge".
indexOf("Thomas Troubridge", "x")
Returns 0, as the sub-string "x" does not exist in the string.
indexOf("Space1, Space2 and Space3", "e")
Returns 5, which is the first occurrence of "e". It does not return subsequent occurrences at positions 13 and 24.
Using indexOf in a script:
do( $start = indexOf(in.fileName, "_") + 1, $end = $start + 8, $date = substring(in.fileName, $start, $end) )
If in.fileName contained file names like "switchRC48_20060708.exp", the script above would extract the date element of the file name.