The STRPOS function finds the first occurrence of a substring within an object string.
Find the position of the string “fun” within the string “IDL is fun” and print the result by entering:
The REVERSE_SEARCH keyword to the STRPOS function makes it easy to find the last occurrence of a substring within a string. In the following example, we search for the last occurrence of the letter “I” (or “i”) in a sentence:
sentence = 'IDL is fun.'
sentence = STRUPCASE(sentence)
lasti = STRPOS(sentence, 'I', /REVERSE_SEARCH)
This results in:
Note that although REVERSE_SEARCH tells STRPOS to begin searching from the end of the string, the STRPOS function still returns the position of the search string from the beginning of the string (where 0 is the position of the first character).
If Search_String occurs in Expression, STRPOS returns the character position of the match, otherwise it returns -1.
The expression in which to search for the substring. If this argument is not a string, it is converted using IDL’s default formatting rules. If Expression is an array, the result is an array with the same structure, where each element contains the position of the substring within the corresponding element Expression. If Expression is an empty string, STRPOS returns the value -1.
The substring to be searched for within Expression. If this argument is not a string, it is converted using IDL’s default formatting rules. If Search_String is an empty string, STRPOS returns the smaller of Pos or one less than the length of Expression.
The character position at which the search is begun. If Pos is omitted and the REVERSE_SEARCH keyword is not set, the search begins at the first character (character position 0). If REVERSE_SEARCH is set, the default is to start at the last character in the string. If Pos is less than zero, zero is used for the starting position.
Normally, the value of Pos is used as an offset from the beginning of the expression towards the end. Set REVERSE_OFFSET to use it as an offset from the last character of the string moving towards the beginning. This keyword makes it easy to position the starting point of the search at a fixed offset from the end of the string. If this keyword is set and Pos is not specified, STRPOS will return the value -1.
STRPOS usually starts at Pos and moves toward the end of the string looking for a match. If REVERSE_SEARCH is set, the search instead moves towards the beginning of the string.