Re: Replacing a substring in a string [message #480010 is a reply to message #479987] |
Thu, 21 March 2013 06:14 |
|
danpaul88
Messages: 5795 Registered: June 2004 Location: England
Karma:
|
General (5 Stars) |
|
|
Depends on a few things. If you just want pointers to both strings and don't mind them staying in the same memory block as each other you can write over the first character of the strstr result with a null character and advance its position by <length of search string> to get the pointer to the second string, using the original pointer as the pointer to the first string. Repeat until there are no more matches as necessary, either acting on each bit of string one at a time or accumulating the pointers in a vector or other storage container (pre-allocate it to a decent size first to avoid reallocations mid-processing as much as possible).
That's just one way of doing it off the top of my head. It's destructive to the source string (you could copy it first if this is a problem) but it allows you to split a string into multiple substrings. This is equivilent to how strtok works.
[Updated on: Thu, 21 March 2013 06:15] Report message to a moderator
|
|
|