replacing string in string 
Author Message
 replacing string in string

What's the simplest way to replace a substring with another (eventually
*growing it* if required)? E.g. replace in the std::string "This is just a
test" all the occurrences of "is" with "ABC" so that the result becomes
"ThABC ABC just a test"?

Thanks!



Wed, 06 Oct 2004 03:47:02 GMT  
 replacing string in string
Try the following

    std::string    sBuffer("This is just a test" );
    std::string    sString1("is" );
    int nOffset ;

    while ( ( nOffset = sBuffer.find(sString1) ) != string::npos )
    {
         sBuffer.replace(nOffset, sString1.size(), "ABC");
    }

    HTH.

--
Cheers
Check Abdoul
----------------


Quote:
> What's the simplest way to replace a substring with another (eventually
> *growing it* if required)? E.g. replace in the std::string "This is just a
> test" all the occurrences of "is" with "ABC" so that the result becomes
> "ThABC ABC just a test"?

> Thanks!



Wed, 06 Oct 2004 04:23:42 GMT  
 replacing string in string
Use CString. It has a lot of functionality, and it's
free :)  Use all the CString your heart desires :P


Quote:
>-----Original Message-----
>What's the simplest way to replace a substring with
another (eventually
>*growing it* if required)? E.g. replace in the

std::string "This is just a
Quote:
>test" all the occurrences of "is" with "ABC" so that the
result becomes
>"ThABC ABC just a test"?

>Thanks!

>.



Tue, 12 Oct 2004 18:11:27 GMT  
 replacing string in string
or you could actually use std::string like the OP asked

basic_string::replace is what your looking for
use it in combination with find_first_of

// sudo code probably won't compile or work right, but you get the idea
std::string myString = "This is just a test";
size_t s = 0;
while(std::npos != (s = myString.find_first_of("is", s)))
{
    myString.replace(s, strlen("is"), "is");

Quote:
}



Quote:
> Use CString. It has a lot of functionality, and it's
> free :)  Use all the CString your heart desires :P


> >-----Original Message-----
> >What's the simplest way to replace a substring with
> another (eventually
> >*growing it* if required)? E.g. replace in the
> std::string "This is just a
> >test" all the occurrences of "is" with "ABC" so that the
> result becomes
> >"ThABC ABC just a test"?

> >Thanks!

> >.



Tue, 12 Oct 2004 22:59:00 GMT  
 replacing string in string


Quote:
> or you could actually use std::string like the OP asked

> basic_string::replace is what your looking for
> use it in combination with find_first_of

I ment find find_first_of would look for the chars i or s not the substr is
Quote:
> // sudo code probably won't compile or work right, but you get the idea
> std::string myString = "This is just a test";
> size_t s = 0;
> while(std::npos != (s = myString.find_first_of("is", s)))
> {
>     myString.replace(s, strlen("is"), "is");
> }



Tue, 12 Oct 2004 23:21:17 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Replace more then one charaster using String.Replace()

2. newbie needs help to strings, strings, strings

3. replacing backslash in String

4. Newbie: Sub-String search and replace routine

5. Newbie: Sub-String search and replace routine

6. Bug in string.Replace (C#)?

7. replace one character in string

8. Please help me improved my string replace function

9. String - search and replace

10. Here's how to do case - insensetive string replace

11. replace a string in a file

12. search and replace strings with lists

 

 
Powered by phpBB® Forum Software