String processing: word replacement

A

anhtt

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
 
G

George Faraj

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
 
J

Jeff Flinn

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
 
J

Jeff Flinn

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
 
C

Computer Whizz

Jeff said:
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

I'd just like to point out here that I tried the Dev C++ (4.9.9.0 I
guess) Devpack of Boost, and for some reason an example or two didn't
work. The exact demo was about the filesystem, doing a simple "ls"
command. I use Windows, but I thought Boost was cross-platform??

Fearful I had left something out, or needed to 'make' my own library I
got rid of it..
Was I jumping the gun a little, as I had added the include path to the
Dev C++ list - I'm trying to make this as generic as possible to not be
off-topic, and understand I'm skating on the thin part.
 
J

Jeff Flinn

Computer Whizz said:
I'd just like to point out here that I tried the Dev C++ (4.9.9.0 I guess)
Devpack of Boost, and for some reason an example or two didn't work. The
exact demo was about the filesystem, doing a simple "ls" command. I use
Windows, but I thought Boost was cross-platform??

Fearful I had left something out, or needed to 'make' my own library I got
rid of it..
Was I jumping the gun a little, as I had added the include path to the Dev
C++ list - I'm trying to make this as generic as possible to not be
off-topic, and understand I'm skating on the thin part.

Sorry I don't know Dev C++. Your best bet is to join the boost user mailing
list. See the boost website for instructions to do so.

Jeff Flinn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top