Visual Basic and Visual FoxPro functions to search strings

VBA

Instr(), InstrRev()

VFP

At(), AtC(), AtCC(), Rat(), Occurs()

Visual Basic Syntax Notes

The VBA function Instr() returns the position of one string within another. This example will return 5 because "E" first occurs as the 5th character of "ABCDEFGHABCDEFGH":

pos = Instr( "ABCDEFGHABCDEFGH"),"E")

If the target can't be found in the string then Instr() will return 0. The search is case-insensitive.

The InstrRev() function will search backwards from the end of the string. This example will return 13 because "E" last occurs as the 13th character of "ABCDEFGHABCDEFGH":

pos = InstrRev( "ABCDEFGHABCDEFGH","E")

Visual FoxPro Syntax Notes

The simple functions At() returns the position of one string within another. This example will return 5 because "E" first occurs as the 5th character of "ABCDEFGHABCDEFGH":

pos = At("E", "ABCDEFGHABCDEFGH")

If the target can't be found in the string then At() will return 0. The search is case-sensitive. Use the FoxPro AtC() function for a case-insensitive search.

The Rat() function will search backwards from the end of the string. This example will return 13 because "E" last occurs as the 13th character of "ABCDEFGHABCDEFGH":

pos = Rat("E", "ABCDEFGHABCDEFGH")

Both the At() and Rat() functions can take a third numeric parameter to specify whether FoxPro should report the first, second or nth instance of the character. This example will return 13 because it is looking for the second occurence of the target and "E" makes its second appearance as the 13th character of "ABCDEFGHABCDEFGH":

pos = At("E", "ABCDEFGHABCDEFGH", 2)

The FoxPro Occurs() function counts the number of times that a character occurs in another string.

Warning

This area of the language is fraught with inconsistencies:

  • The order of the parameters is reversed in the two languages.
  • FoxPro is case-sensitive, Visual Basic isn't.
  • Both languages have an optional third parameter. Visual Basic specifies that the search should start from the nth position; Visual FoxPro specifies that the search should return the nth occurrence.

Trimming spaces  |  Text functions  |  Changing Case