Name

InStr, InStrB Functions

Syntax

InStr([start, ]stringtosearch, stringtofind[, comparemode])
start

Use: Optional

Data Subtype: Numeric

The starting position for the search.

stringtosearch

Use: Required

Data Subtype: String

The string being searched.

stringtofind

Use: Required

Data Subtype: String

The string being sought.

comparemode

Use: Optional

Data Subtype: Integer

The type of string comparison.

Return Value

A variant of subtype Long.

Description

Finds the starting position of one string within another.

Rules at a Glance

  • The return value of InStr is influenced by the values of stringtosearch and stringtofind, as shown in the following table:

    Condition

    InStr Return Value

    stringtosearch is zero-length

    0

    stringtosearch is Null

    Null

    stringtofind is zero-length

    start

    stringtofind is Null

    Null

    stringtofind is not found

    0

    stringtofind is found within stringtosearch

    Position at which the start of stringtofind is found

    start > len(stringtofind)

    0

  • If the start argument is omitted, InStr commences the search with the first character of stringtosearch.

  • If the start argument is Null, an error occurs.

  • You must specify a start argument if you are specifying a comparemode argument.

  • VBScript supports intrinsic constants for comparemode, as follows:

    Comparison Mode

    Value

    Constant

    Binary (default)

    0

    vbBinaryCompare

    Text—case insensitive

    1

    vbTextCompare

    In effect, a binary comparison means that the search for stringtofind in stringtosearch is case-sensitive. A text ...

Get VBScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.