By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

String processing: word replacement

P: n/a
Hi,

I am a newbie in C++. I wonder if there is a standard lib function do
this sort of thing:

string str = " I am XXX, hence I am YYY";

str = replace(str, "XXX", "lazy"); // str now is " I am lazy, hence I
am YYY"
str = replace(str, "YYY", "zzzzing"); // str now is "I am lazy, hence I
am zzzing"

Many Thanks!
Tuan-Anh

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Not that I know of. You have to use string::find in combination with
string::replace to replace a substring like that. Make yourself a helper
function that does it, not very hard.

George Faraj

<an***@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi,

I am a newbie in C++. I wonder if there is a standard lib function do
this sort of thing:

string str = " I am XXX, hence I am YYY";

str = replace(str, "XXX", "lazy"); // str now is " I am lazy, hence I
am YYY"
str = replace(str, "YYY", "zzzzing"); // str now is "I am lazy, hence I
am zzzing"

Many Thanks!
Tuan-Anh

Jul 23 '05 #2

P: n/a

<an***@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi,

I am a newbie in C++. I wonder if there is a standard lib function do
this sort of thing:

string str = " I am XXX, hence I am YYY";

str = replace(str, "XXX", "lazy"); // str now is " I am lazy, hence I
am YYY"
str = replace(str, "YYY", "zzzzing"); // str now is "I am lazy, hence I
am zzzing"

See http://www.boost.org/doc/html/string_algo.html

#include "boost/algorithm/string/replace.hpp"

using boost::algorithm;

....

string str = " I am XXX, hence I am YYY";

replace_first(str, "XXX", "lazy"); // str now is " I am lazy, hence I am
YYY"
replace_first(str, "YYY", "zzzzing"); // str now is "I am lazy, hence I am
zzzing"

Jeff Flinn
Jul 23 '05 #3

P: n/a
Thanks, the link is very useful, I am not aware of Boost. Quite a
newbie in C++.

Jul 23 '05 #4

P: n/a

<an***@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Thanks, the link is very useful, I am not aware of Boost. Quite a
newbie in C++.


Newbie or not, I find boost indispensable. You'll probably find that for
many common tasks, boost provides the facilities that you would have had to
develop on your own. Most of the libraries do not require you to build
boost. You can just download and extract to something like
'C:/boost/boost_1_32_0'. Then add this to your include path. If you use this
approach, handling updates is much simpler.

Have fun.

Jeff
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.