ASPFAQs.com
Your source for ASP-related frequently asked questions and answers!


4GuysFromRolla.com : ASP FAQS : Strings

Question: How can I find the position of a certain character (or substring) within a string?

Answer: Use the InStr function. The InStr function has the following definition:

InStr(start, StringToSearch, StringToFind [, compare])

start specifies where to begin searching the StringToSearch looking for StringToFind. If you want to start searching at the beginning of the string, set start to 1.

The optional compare parameter specifies if a binary or textual compare should be performed. If you set compare to the system constant vbBinaryCompare (or the hardcoded value of 0) then a binary search will be performed; setting compare to vbTextCompare (or the hardcoded value of 1), a textual compare will be performed. The difference is as follows: a binary search takes case into consideration while a textual comparison does not. So, if you were seaching for the substring "foo" in the string "FooBar", a textual comparison would find "foo" while a binary comparison would not. (By default, a binary comparison is performed.)

Dim str
str = "No beer and no TV makes Homer something-something."

Response.Write InStr(1, str, "Homer") & "<BR>"
Response.Write InStr(1, str, "homer") & "<BR>"
Response.Write InStr(1, str, "homer", vbTextCompare) & "<BR>"
Response.Write InStr(1, str, "TV") & "<BR>"
Response.Write InStr(1, str, "Scott") & "<BR>"
Response.Write InStr(1, str, "no", vbBinaryCompare) & "<BR>"
Response.Write InStr(1, str, "no", vbTextCompare) & "<BR>"

The output from the above script is:
25
0
25
16
0
13
1

For more information on InStr, be sure to read the technical docs! Happy Programming!


FAQ posted by Scott Mitchell at 9/24/2000 3:38:32 PM to the Strings category. This FAQ has been viewed 65,717 times.


Copyright 2019 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers