Routines (alphabetical) > Routines: S > STRPOS

# STRPOS

Tip: See also the IDL_String::IndexOf and IDL_String::LastIndexOf methods, which provide similar functionality but with an object-oriented interface.

The STRPOS function finds the first occurrence of a substring within an object string.

## Examples

Find the position of the string “fun” within the string “IDL is fun” and print the result by entering:

PRINT, STRPOS('IDL is fun', 'fun')

IDL prints:

7

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)
PRINT, lasti

This results in:

4

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).

## Syntax

Result = STRPOS( Expression, Search_String [, Pos], /REVERSE_OFFSET, /REVERSE_SEARCH )

## Return Value

If Search_String occurs in Expression, STRPOS returns the character position of the match, otherwise it returns -1.

## Arguments

### Expression

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.

### Search_String

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.

### Pos

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.

## Keywords

### REVERSE_OFFSET

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.

### REVERSE_SEARCH

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.

## Version History

Original | Introduced

## See Also

String Operations, String Processing, STRMID, STRPUT, STRTRIM, IDL_String