How to Use the instr Function

Source: Internet
Author: User

The key to implementing the search function is to use the instr function, which can locate the first position of the specified string in another string. Let's take a look at the syntax for using this function:

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

The parameters required for this function are the starting position, the subject string, and the string to be searched. Compare is an optional parameter. String comparison. This compare parameter can be omitted, or 0, 1, or 2. Specify 0 (default) for Binary comparison. Specify 1 for case-insensitive text comparison. For example, if we want to find whether "cd" exists in the string "abcdefg" and return its location, we can use the following statement:

Pos = instr (1, "abcdefg", "cd ")

The POS returns 3 to indicate that the position is found and starts with the third character. This is the implementation of "Search", and the implementation of the "Find Next" function is to use the current position as the starting position to continue searching.

The following is an example:

Put a text box text1 for the user to enter text or call the text file, used to verify the search text in it, put another text box text2 for the user to enter the string to search for, put two command buttons, command1 and command2 are titled "Search" and "find next" respectively ".

Write the following code in the overall declaration section of the form:

Option explicit 'defines the target location variable
Private targetposition as integer

'Compile a lookup Function
Private sub findtext (byval start_at as integer)
Dim POS as integer
Dim target as string
'Get the string that the user enters to search
Target = text2.text
Pos = instr (start_at, text1.text, target)
If POS> 0 then
'Matched string found
Targetposition = POS
Text1.selstart = targetposition-1
'Selected string
Text1.sellength = Len (target)
Text1.setfocus
Else' no matching string found
Msgbox "not found! "
Text1.setfocus
End if
End sub

'Double-click the find command button:
Private sub commandateclick () 'starts from the first character
Findtext 1
End sub

'Double-click the Find Next button:
Private sub command2_click () 'continues searching from the current position
Findtext targetposition + 1
End sub

Run the program, enter some strings in the text box 1, enter the string to be searched in the text box 2, and click "Search" and "find next" to verify.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.