Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Finding the nth occurrence of a substring


Reply
 
Thread Tools Display Modes
  #1  
Old 05-03-2004, 09:41 AM
Raystoid Raystoid is offline
Newcomer
 
Join Date: Apr 2004
Posts: 14
Default Finding the nth occurrence of a substring


I want to find, for instance, the position of the 13th occurance of the substring ">" in the string str_data. To do this, i have constructed the following code:

For i = 0 To 12
posTimeStart = InStr(posTimeStart, str_data, ">")
Next

However, when I attempt to run, I get the following error:
"Invalid procedure call or argument".

It seems like this should work. I am able to find the position of another substring in a command that occurs before this one (posRouteID = InStr(str_data, routePrefix & "144")), though.
Reply With Quote
  #2  
Old 05-03-2004, 09:48 AM
John's Avatar
John John is offline
Bit Flipper
 
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
Default

What is the value of posTimeStart when that error occurs?

Also you should do this: posTimeStart = InStr(posTimeStart + 1, str_data, ">")
So you don't get the same one each time through the loop.
__________________
Subclassing|Magnetic Forms|Operator Overloading (VB2K5)|QuickSnip.NET

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Reply With Quote
  #3  
Old 05-03-2004, 09:58 AM
puccij puccij is offline
Freshman
 
Join Date: Apr 2004
Location: Columbus Ohio
Posts: 31
Default Find nth Occurance

There were two problems with your code.

Usage: The first parameter of the instr function must be a value between 1 and the length of the seconcond parameter. 0 doesn't work.

Logical: Your code would search the string 12 timjes starting at each of the first 12 characters (assumming you start you index at 1 instead of zero). This is not what you say you want.


There may be an easier way but this works:

Function GetNthOccurancePos(n As Integer, SearchStr As String, FindStr As String) as integer

Dim i As Integer
Dim FindCt As Integer
i = 1
Do Until i = 0 Or FindCt = n
i = InStr(i, SearchStr, FindStr)
If i > 0 Then
FindCt = FindCt + 1
i = i + 1
End If
Loop
GetNthOccurancePos = i - 1
End Function
Reply With Quote
  #4  
Old 05-03-2004, 10:00 AM
John's Avatar
John John is offline
Bit Flipper
 
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
Default

Quote:
Originally Posted by puccij
...
Logical: Your code would search the string 12 timjes starting at each of the first 12 characters (assumming you start you index at 1 instead of zero). This is not what you say you want.
...
He's not using i as a paramter to InStr. He's using posTimeStart which will contain the index of the last occurance found in the loop. Adding 1 to it should fix the problem assuming it is initialised to 0.
__________________
Subclassing|Magnetic Forms|Operator Overloading (VB2K5)|QuickSnip.NET

"These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Reply With Quote
  #5  
Old 05-03-2004, 10:26 AM
puccij puccij is offline
Freshman
 
Join Date: Apr 2004
Location: Columbus Ohio
Posts: 31
Default oops

Quote:
Originally Posted by John
He's not using i as a paramter to InStr. He's using posTimeStart which will contain the index of the last occurance found in the loop. Adding 1 to it should fix the problem assuming it is initialised to 0.

You are correct on both counts - thx.

The code I wrote may still be useful because it generalizes the case
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain a substring from a string cybernerdsx2 General 8 01-19-2004 12:35 AM
searching for substring in field in access Durgon Database and Reporting 3 08-12-2003 11:56 PM
a star path finding felisitas_nike Game Programming 3 07-06-2003 12:12 PM
Finding My Child vbNEMESIS API 9 01-13-2003 11:11 AM
Substring help, please sober_analyst General 1 04-02-2001 12:10 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->