Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 11,809 times

Contents

    An Introduction to String Handling in VB6 - Using InStr

    sync_or_swim

    Using InStr

    Example 1 - Determining if a string contains a certain substring using the InStr Function

    There are many uses for the InStr function to give a basic example we shall use it to ascertain whether or not a string contains any spaces.  The function accepts four parameters:

    The position to start searching from
    The string to be searched
    The string to search for
    The comparison method (Optional), by default this will be a binary comparison

    Syntax
    InStr([Start Position, ]String to Check, String to Match[, Compare Method])

    Return Value
    The function returns a numeric value, if the search string is not found within the specified string this value will be Zero however, if the string is found, the function will be the starting
    position of the first occurence of the substring.

    Usage
    The two common usages for the InStr function are:
    a) To discover the starting position of one string within another

    Dim intPos As Integer
    intPos = InStr(1, strString, "example")


    The variable intPos will now contain the starting pos of the string "example" which is 18

    b) To ascertain whether a string is present within another before we do something

    If InStr(1, strString, "example") Then
        'string contains the substring "example"
    Else
        'string does not contain the substring "example"
    End If

    This is useful for performing a check on a string to make sure that it contains the parameter we need before we perform string manipulation upon it.  An example of how this would be used may be to make sure that the given string contains one or more spaces before we perform an operation upon it which relies on there being spaces present:

    If InStr(1, strString, " ") Then
        'Perform some operation
    End If

    Comments