String.IndexOf

From Xojo Documentation

Revision as of 21:11, 6 January 2020 by PLefebvre (talk | contribs) (Sample Code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Method

String.IndexOf([start As Integer,] find As String, [options As ComparisonOptions = ComparisonOptions.CaseInsensitive,] [locale as Locale]) As Integer

New in 2019r2

Supported for all project types and targets.

Returns the position of the first occurrence of a string inside another string.

Parameters

Part Type Description
returns Integer The position of the first occurrence of find in source. If the search string cannot be located in source, IndexOf returns -1.
start Integer Optional position from which to begin searching the source string.
find String Required. String expression being sought.
options ComparisonOptions Optional. Dictates whether or not the comparison will be case-sensitive. The default is case-insensitive.
locale Locale Optional. Dictates whether or not a locale should be considered. The default is to use the current locale.

Notes

If the find string is not found within the source string, -1 is returned. If the find string is an empty string, then start is returned. That is, myString.IndexOf("") returns -1 and myString.IndexOf(3,"") returns 3.

IndexOf is case-insensitive by default, even with accented Roman characters and non-Roman alphabets. If you wish to make a case-sensitive search, use the optional ComparisonOptions parameter.

If you need to find the byte position of the find string within the source string, use a MemoryBlock instead.

Sample Code

This example uses the IndexOf function to locate a string within another string.

Var first As Integer
Var source As String = "This is a test"
first = source.IndexOf("t") // returns 0
first = source.IndexOf("is") // returns 2
first = source.IndexOf(4, "is") // returns 5
first = source.IndexOf("tester") // returns -1
first = source.IndexOf(0, "IS", ComparisonOptions.CaseSensitive) // returns -1

See Also

String for a complete list of functions.