?
Solved

Finding second occurrence of a string with instr

Posted on 2009-12-21
6
Medium Priority
?
7,160 Views
Last Modified: 2013-11-08
I have a long string and want to find the 2nd occurrence of "AND" in this string:
abc = '123' AND def = '456' AND ghi = '789'

I use instr to find the position of the first AND.  How can I get the same info on the 2nd?
0
Comment
Question by:kkbenj
6 Comments
 
LVL 8

Accepted Solution

by:
milindsm earned 2000 total points
ID: 26095411
' String to search in.
Dim SearchString As String = "abc = '123' AND def = '456' AND ghi = '789'"

' Search for "AND".
Dim SearchChar As String = "AND"

Dim TestPos As Integer

TestPos = InStr(1, SearchString, SearchChar, CompareMethod.Text)

' A binary comparison starting at position 1. Returns 9.
if TestPos > 0 Then
TestPos = InStr(TestPos, SearchString, SearchChar, CompareMethod.Binary)
End if

MsgBox "2nd Occurrence is at " & TestPos
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 26095416
Hello kkbenj,

You could use split and:

split("abc = '123' AND def = '456' AND ghi = '789'", "AND")(2)

Regards,

chris_bottomley
0
 
LVL 8

Expert Comment

by:milindsm
ID: 26095420
Code Snippet...!
' String to search in.
Dim SearchString As String = "abc = '123' AND def = '456' AND ghi = '789'"

' Search for "AND".
Dim SearchChar As String = "AND"

Dim TestPos As Integer

TestPos = InStr(1, SearchString, SearchChar, CompareMethod.Text)

if TestPos > 0 Then
TestPos = InStr(TestPos, SearchString, SearchChar, CompareMethod.Binary)
End if

MsgBox "2nd Occurrence is at " & TestPos

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Mcp
ID: 26095447
Add Instr in a loop, use Instr incremented by +1 for each character in string to find your search string.
0
 

Author Closing Comment

by:kkbenj
ID: 31668495
Thanks for such a quick solution
0
 

Author Comment

by:kkbenj
ID: 26098045
Idle_Mind - can I ask why you used both CompareMethod binary and text?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question