Siebel VB Language Reference > VB Language Reference >

InStr Function


This standard VB function returns the position of the first occurrence of one string within another string.

Syntax A

InStr([start,] string1, string2)

Syntax B

InStr(start, string1, string2[, compare])

Argument
Description

start

An integer representing the position in string1 to begin the search, with the first character in the string as 1

string1

The string to search

string2

The string to find

compare

0 if a case-sensitive search is desired

1 if a case-insensitive search is desired

Returns

The position of the first character of string2 in string1.

Usage

If not specified, the search starts at the beginning of the string (equivalent to a start of 1). These arguments can be of any type. They are converted to strings.

InStr returns a zero under the following conditions:

  • start is greater than the length of string2.
  • string1 is a null string.
  • string2 is not found.

If either string1 or string2 is a null variant, Instr returns a null variant.

If string2 is a null string (""), Instr returns the value of start.

If compare is 0, a case-sensitive comparison based on the ANSI character set sequence is performed. If compare is 1, a case-insensitive comparison is done based upon the relative order of characters as determined by the country code setting for your computer. If compare is omitted, the module level default, as specified with Option Compare, is used.

Example

This example generates a random string of characters, then uses InStr to find the position of a single character within that string.

Sub Button_Click
   Dim x as Integer
   Dim y
   Dim str1 as String
   Dim str2 as String
   Dim letter as String
   Dim randomvalue
   Dim upper, lower
   Dim position as Integer
   Dim msgtext, newline
   upper = Asc("z")
   lower = Asc("a")
   newline = Chr(10)
   Randomize
   For x = 1 to 26
      randomvalue = Int(((upper - (lower + 1)) * Rnd) + lower)
      letter = Chr(randomvalue)
      str1 = str1 & letter
'Need to waste time here for fast processors
      For y = 1 to 1000
      Next y
   Next x
   str2 = "i"
   position = InStr(str1,str2)
   If position then
      msgtext = "The position of " & str2 & " is: " _
         & position & newline & "in string: " & str1
   Else
      msgtext = "The letter: " & str2 & " was not found in: " _
         & newline
      msgtext = msgtext & str1
      End If
End Sub

Related Topics

Left Function
Mid Function
Mid Statement
Option Compare Statement
Right Function
Str Function
StrComp Function

Siebel VB Language Reference Copyright © 2006, Oracle. All rights reserved.