Searching a String

InStr and InStrRev are two Visual Basic functions to locate one string inside another. InStr locates the first occurrence of the string, and InStrRev locates the last occurrence.

Code: VB

Const strExample AsString = "Please try to do what you can do"Dim pos AsIntegerpos = InStr(strExample, "do")If pos > 0ThenMe.Print"Found first occurrence of string at position " & posElseMe.Print"String not found"EndIf

Returns, "Found first occurrence of string at position 15"

Code: VB

Const strExample AsString = "Please try to do what you can do"Dim pos AsIntegerpos = InStrRev(strExample, "do")If pos > 0ThenMe.Print"Found last occurrence of string at position " & posElseMe.Print"String not found"EndIf

I think that the Mid function is often used whenever you need to extract a certain string from a big string. I often do it in the programs I do. You extract only what you want and then you work with that string or display it to the user in a listbox, combo box, etc..

Luis Lazo

Last edited by shabbir; 1May2008 at 11:28..
Reason: Confine links to signature