473,804 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String processing: word replacement

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
4 1781
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******** *************@f 14g2000cwb.goog legroups.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

<an***@hotmail. com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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::algorith m;

....

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

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

Jeff Flinn
Jul 23 '05 #3
Thanks, the link is very useful, I am not aware of Boost. Quite a
newbie in C++.

Jul 23 '05 #4

<an***@hotmail. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4899
by: Juha Suni | last post by:
Hi! I have managed to live without using too much regular expressions so far, and now that I need one, I need some help too. I have a string containing a (possibly large) block of html. I need to insert code (an image to be precise) right after the last textual character in the string. This is, of course, no problem if the end contains plain text and no html. If the string ends in HTML however, I need to fiddle around. for example...
19
78850
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not find one.
0
2898
by: David Krmpotic | last post by:
Regards, I need a little explanation in converting word VB macros to charp code! first, I recorded simple macro which replaces every occurence of one word in whole document (is it?) , here: Sub replaceall() '
6
4745
by: Alexander Muylaert | last post by:
Hi Does anyone know a good starting point about high speed string processing in C#? What I need is a very fast routine for a case insensitive "contains". e == E == é == ë == ... Kind regards
35
5839
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a collective brainstorming session. Let's discuss some C here, for a change :-)
23
9528
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP will print literally as {{-Hello}} World, instead of
8
4218
by: John Salerno | last post by:
I figured my first step is to install the win32 extension, which I did, but I can't seem to find any documentation for it. A couple of the links on Mark Hammond's site don't seem to work. Anyway, all I need to do is search in the Word document for certain strings and either delete them or replace them. Easy enough, if only I knew which function, etc. to use. Hope someone can push me in the right direction.
1
9080
by: Michael Yanowitz | last post by:
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the string.replace(from_string, to_string, len(string)) to replace names in a file with their IP address. For example, I have definitions file, that looks something like: 10.1.3.4 LANDING_GEAR 20.11.222.4 ALTIMETER_100
3
5912
by: jcassan | last post by:
Hello folks. I am new to these forums and have something, which has been stumping me for little while. I am using pspell to spellcheck a scrolling textbox (textarea) containing user input. I analyze each word and rebuild the text string as a combination of correct text (not misspelled) and a drop down list offering suggestions in place of any misspelled words. Pretty common stuff using pspell. I display the new string so that the user...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.