INSTR


Locates the first occurrence of a substring or character within a string

Syntax:
declare function Instr ( byref str as const string, [ Any ] byref substring as const string ) as integer
declare function Instr ( byref str as const wstring, [ Any ] byref substring as const wstring ) as integer
declare function Instr ( byval start as integer, byref str as const string, [ Any ] byref substring as const string ) as integer
declare function Instr ( byval start as integer, byref str as const wstring, [ Any ] byref substring as const wstring ) as integer

Usage:
first = Instr( [ start, ] str, [ Any ] substring )

Parameters:
str
The string to be searched.
substring
The substring to find.
start
The position in str at which the search will begin. The first character starts at position 1.

Return Value:
The position of the first occurrence of substring in str.

Description:
Locates the position of the first occurrence of a substring or character within a string. In the first form of Instr (without start parameter), the search begins at the first character.

Zero (0) is returned if: either substring is not found, either str or substring are empty strings, or start < 1.

If the Any keyword is specified, Instr returns the first occurrence of any character in substring.

Examples:
' It will return 4
Print InStr("abcdefg", "de")

' It will return 0
Print InStr("abcdefg", "h")

' It will search for any of the characters "f", "b", "c", and return 2 as "b" is encountered first
Print InStr("abcdefg", Any "fbc")

Dim test As String
Dim idx As Integer

test = "abababab"
idx = InStr(test, "b")

Do While idx > 0 'if not found loop will be skipped
    Print """b"" at " & idx
    idx = InStr(idx + 1, Test, "b")
Loop

'A Unicode example:
dim text as wstring*20
text = "Привет, мир!"
print instr(text,"ет") ' displays 5
 

Platform Differences:
Differences from QB:
See also:
Back to String Functions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode