The VB6 syntax works in my VB.net
Main Topics
Browse All TopicsWhat happened to the InStr Function in VB.NET? There is no starting position for the function. My question is how would I find the 2nd, 3rd etc. ocurrence in a string. Thanks for the help.
In VB6 I would simply do something like:
Dim tag_open As Integer
Dim tag_close As Integer
Dim str As String
str = "[This is a Test],[This is a test 2]"
tag_close = 1
Do
tag_open = InStr(tag_close, str, "[")
If tag_open = 0 Then Exit Do
tag_open = tag_open + 1
tag_close = InStr(tag_open, str, "]")
Debug.Print Mid(str, tag_open, tag_close - tag_open)
'tag_close = tag_open + 1
Loop
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The Instr and InStrRev functions are still supported in .NET, but they are not the official way to handle strings.
The IndexOf and LastIndexOf functions are available for every string in .NET. So, for example:
Dim i as Integer
'Use the "IndexOf" method with a regular string.
i = "My String".IndexOf("String")
'Use the "IndexOf" method with a string variable.
Dim str as String = "This is another string"
i = str.IndexOf("string")
'Use the "IndexOf" method with a TextBox, because the "Text" property is a String.
Dim tb As New TextBox
tb.Text = "my string"
i = tb.Text.IndexOf("string")
'Here is a LastIndexOf example for you:
Dim strFilePath As String = "C:\Temp\MyFile.txt"
Dim i As Integer
i = strFilePath.LastIndexOf("\
' i now equals the integer 7: the exact position of the last "\" (see notes below).
** Only 2 points to be aware of as you start using IndexOf and LastIndexOf:
1. If no match is found, then a -1 is returned, not a 0 as with the Instr and InstrRev functions.
'Here, a -1 will be returned, because the question mark is not found.
i = "My String".IndexOf("?")
2. Strings are zero (0) based in .NET.
'So for example, the integer "3" will be returned,
' because the first character in the string "M" is position 0.
i = "My String".IndexOf("String")
One final note:
'If you want to search for an exact match, then use the default,
i = "My String".IndexOf("String")
'However, if you want to search for a string, but you want to ignore the case
'such as UPPER CASE, Mixed Case, and lower case, then make sure you use the following:
i = "My String".IndexOf("string", StringComparison.CurrentCu
So, in summary:
- If you used the Instr method in VB6, use the IndexOf method in .NET.
- If you used the InstrRev method in VB6, use the LastIndexOf method in .NET.
Business Accounts
Answer for Membership
by: Idle_MindPosted on 2007-05-07 at 12:23:40ID: 19045245
Use String.IndexOf(): /en-us/lib rary/d93tk zah.aspx
http://msdn2.microsoft.com