digitalmars.D.learn - n ocurrance in a string
- Heinz (5/5) Jan 02 2007 Hi,
- BCS (9/15) Jan 02 2007 slice off the front:
- Heinz (6/6) Jan 02 2007 Hi, thanks for your reply. But with this method i'd have to repeat it n ...
- David Medlock (11/19) Jan 04 2007 A few ideas that popped into my head.
- Heinz (6/21) Jan 02 2007 Man, i found a bug in the method you suggest while trying to code it:
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
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
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
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
== Quote from BCS (nothing pathlink.com)'s articleHeinz wrote: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? ThxHi, 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
== Quote from Heinz (malagana15 yahoo.es)'s article== Quote from BCS (nothing pathlink.com)'s articleHahaha, hey man i didn't saw the +=, so your code is correct, i'm sorry. Thanks a lot man.Heinz wrote: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? ThxHi, 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
Heinz wrote:== Quote from Heinz (malagana15 yahoo.es)'s articleI spoke to soon (see other post) :b Glad to help!== Quote from BCS (nothing pathlink.com)'s articleHahaha, hey man i didn't saw the +=, so your code is correct, i'm sorry. Thanks a lot man.Heinz wrote: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? ThxHi, 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
Heinz wrote:== Quote from BCS (nothing pathlink.com)'s articleThat 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.Heinz wrote: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? ThxHi, 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