www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - n ocurrance in a string

reply Heinz <malagana15 yahoo.es> writes:
Hi,

I'm doing a bit of work with strings. There's a function called find in
std.string. This function returns the first ocurrance of a substring in a
string, but how can i get the second, thrith, fifth...Nth ocurrance?
Thanks a lot.
Jan 02 2007
parent reply BCS <nothing pathlink.com> writes:
Heinz wrote:
 Hi,
 
 I'm doing a bit of work with strings. There's a function called find in
 std.string. This function returns the first ocurrance of a substring in a
 string, but how can i get the second, thrith, fifth...Nth ocurrance?
 Thanks a lot.
slice off the front: char[] string = ... int i = FindInString(string, "whatever"); i++; i += FindInString(string[i..$], "whatever"); You may want to change this a bit for checking the return value (test of no matche) and starting then next search more than one part the last (to avoid overlapping matches.
Jan 02 2007
next sibling parent reply Heinz <malagana15 yahoo.es> writes:
Hi, thanks for your reply. But with this method i'd have to repeat it n times,
if
i want to find the 124th ocurrance, that would take a lot of time right?

Anyway i find it a straight foward method, i think it's the best way to go. This
is a very needed function, i'll add a counter to check the n times it has to
loop.
I'll post it then (someone may need it).

Thanks in advance.
Jan 02 2007
parent David Medlock <noone nowhere.com> writes:
Heinz wrote:
 Hi, thanks for your reply. But with this method i'd have to repeat it n times,
if
 i want to find the 124th ocurrance, that would take a lot of time right?
 
 Anyway i find it a straight foward method, i think it's the best way to go.
This
 is a very needed function, i'll add a counter to check the n times it has to
loop.
 I'll post it then (someone may need it).
 
 Thanks in advance.
A few ideas that popped into my head. Search for the last character in your substring, say you find it at N when you find it check for your substring at N - substring.length. Perhaps as you cycle through the string, keep a sum of the last X character values, where X is the length of your substring. If your current SUM is not equal to the sum of the characters in your substring you don't have to perform the check above. Perhaps check citeseer for string searching algorithms. Good luck. -DavidM
Jan 04 2007
prev sibling parent reply Heinz <malagana15 yahoo.es> writes:
== Quote from BCS (nothing pathlink.com)'s article
 Heinz wrote:
 Hi,

 I'm doing a bit of work with strings. There's a function called find in
 std.string. This function returns the first ocurrance of a substring in a
 string, but how can i get the second, thrith, fifth...Nth ocurrance?
 Thanks a lot.
slice off the front: char[] string = ... int i = FindInString(string, "whatever"); i++; i += FindInString(string[i..$], "whatever"); You may want to change this a bit for checking the return value (test of no matche) and starting then next search more than one part the last (to avoid overlapping matches.
Man, i found a bug in the method you suggest while trying to code it: The returned int is no longer relative to beginning of the original string because every time you call this find function it takes a diferent string as parameter. Any other ideas? Thx
Jan 02 2007
next sibling parent reply Heinz <malagana15 yahoo.es> writes:
== Quote from Heinz (malagana15 yahoo.es)'s article
 == Quote from BCS (nothing pathlink.com)'s article
 Heinz wrote:
 Hi,

 I'm doing a bit of work with strings. There's a function called find in
 std.string. This function returns the first ocurrance of a substring in a
 string, but how can i get the second, thrith, fifth...Nth ocurrance?
 Thanks a lot.
slice off the front: char[] string = ... int i = FindInString(string, "whatever"); i++; i += FindInString(string[i..$], "whatever"); You may want to change this a bit for checking the return value (test of no matche) and starting then next search more than one part the last (to avoid overlapping matches.
Man, i found a bug in the method you suggest while trying to code it: The returned int is no longer relative to beginning of the original string because every time you call this find function it takes a diferent string as parameter. Any other ideas? Thx
Hahaha, hey man i didn't saw the +=, so your code is correct, i'm sorry. Thanks a lot man.
Jan 02 2007
parent BCS <nothing pathlink.com> writes:
Heinz wrote:
 == Quote from Heinz (malagana15 yahoo.es)'s article
 == Quote from BCS (nothing pathlink.com)'s article
 Heinz wrote:
 Hi,

 I'm doing a bit of work with strings. There's a function called find in
 std.string. This function returns the first ocurrance of a substring in a
 string, but how can i get the second, thrith, fifth...Nth ocurrance?
 Thanks a lot.
slice off the front: char[] string = ... int i = FindInString(string, "whatever"); i++; i += FindInString(string[i..$], "whatever"); You may want to change this a bit for checking the return value (test of no matche) and starting then next search more than one part the last (to avoid overlapping matches.
Man, i found a bug in the method you suggest while trying to code it: The returned int is no longer relative to beginning of the original string because every time you call this find function it takes a diferent string as parameter. Any other ideas? Thx
Hahaha, hey man i didn't saw the +=, so your code is correct, i'm sorry. Thanks a lot man.
I spoke to soon (see other post) :b Glad to help!
Jan 02 2007
prev sibling parent BCS <nothing pathlink.com> writes:
Heinz wrote:
 == Quote from BCS (nothing pathlink.com)'s article
 Heinz wrote:
 Hi,

 I'm doing a bit of work with strings. There's a function called find in
 std.string. This function returns the first ocurrance of a substring in a
 string, but how can i get the second, thrith, fifth...Nth ocurrance?
 Thanks a lot.
slice off the front: char[] string = ... int i = FindInString(string, "whatever"); i++; i += FindInString(string[i..$], "whatever"); You may want to change this a bit for checking the return value (test of no matche) and starting then next search more than one part the last (to avoid overlapping matches.
Man, i found a bug in the method you suggest while trying to code it: The returned int is no longer relative to beginning of the original string because every time you call this find function it takes a diferent string as parameter. Any other ideas? Thx
That is why the second find use an += rather than a +, that way it walks i through the string. finding "i" in deikilihhgsdf round find returns i 1 2 3 (remember the i++) 2 1 5 3 1 7 It still might not work (I haven't tested it yet) but something along that line should. As to the overhead cost, if you want to find each occurrence in order you wont want to put it in a function, just put it at the top of a loop. That shouldn't be much worse than optimum.
Jan 02 2007