473,785 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I replace all occurrences of a character with another character in std string?


How can I replace all occurrences of a character with another
character in std string?

For example, I want to replace '/' with '+' in my std::string

I have looked at the replace() method in the string class, it does not
replace all occurrences of a character with another character.

http://www.cppreference.com/cppstring/index.html

Thank you

Aug 30 '07 #1
5 25529
herman wrote:
How can I replace all occurrences of a character with another
character in std string?

For example, I want to replace '/' with '+' in my std::string

I have looked at the replace() method in the string class, it does not
replace all occurrences of a character with another character.
You either have to roll your own, or look at a regular expression
library like boost.regexp:

http://www.boost.org/libs/regex/doc/introduction.html

--
Ian Collins.
Aug 30 '07 #2

"herman" <He************ @gmail.comwrote in message
news:11******** *************@o 80g2000hse.goog legroups.com...
>
How can I replace all occurrences of a character with another
character in std string?

For example, I want to replace '/' with '+' in my std::string
Try using std::for_each with a functor. For example

struct testfunctor
{
void operator()(char & c) { if(c == '/') c = '+'; }
};
....
std::string str("This is a test - /////////\n");
std::cout << str;
std::for_each( str.begin(), str.end(), testfunctor() );
std::cout << str;

Aug 30 '07 #3
On Aug 30, 9:39 am, herman <Herman.Schu... @gmail.comwrote :
How can I replace all occurrences of a character with another
character in std string?

For example, I want to replace '/' with '+' in my std::string

I have looked at the replace() method in the string class, it does not
replace all occurrences of a character with another character.

http://www.cppreference.com/cppstring/index.html

Thank you
var str;
str.replace(///g,'+');

/g is for universal char i.e all char in string.

Aug 30 '07 #4

herman <He************ @gmail.comwrote in message...
>
How can I replace all occurrences of a character with another
character in std string?

For example, I want to replace '/' with '+' in my std::string

I have looked at the replace() method in the string class, it does not
replace all occurrences of a character with another character.

http://www.cppreference.com/cppstring/index.html

Thank you
// #include <algorithm>
std::string rep( "?Hello?, ?World?" );
std::replace( rep.begin(), rep.end(), '?', '+' );
cout<<"replace ="<<rep<<std::e ndl;
// out: replace =+Hello+, +World+

--
Bob R
POVrookie
Aug 30 '07 #5
On 2007-08-30 08:09, di*****@gmail.c om wrote:
On Aug 30, 9:39 am, herman <Herman.Schu... @gmail.comwrote :
>How can I replace all occurrences of a character with another
character in std string?

For example, I want to replace '/' with '+' in my std::string

I have looked at the replace() method in the string class, it does not
replace all occurrences of a character with another character.

http://www.cppreference.com/cppstring/index.html

Thank you

var str;
str.replace(///g,'+');

/g is for universal char i.e all char in string.
The other people in this group use the C++ language, what do you use?

--
Erik Wikström
Aug 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2255
by: andrea.gavana | last post by:
Hello NG, probably this is a basic question, but I'm going crazy... I am unable to find an answer. Suppose that I have a file (that I called "Errors.txt") which contains these lines: MULTIPLY 'PERMX' @PERMX1 1 34 1 20 1 6 / 'PERMX' @PERMX2 1 34 21 41 1 6 / 'PERMX' @PERMX3 1 34 1 20 7 14 /
3
30410
by: o_swas | last post by:
Hello, I have a JavaScript string. I want to replace all consecutive occurrences of whitespace characters like spaces, tabs, newlines, and form feeds with another string. For example, say I have a string consisting of: -- 3 spaces -- The characters "hello"
3
3504
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i read in msdn : and notice that trim only Removes all occurrences of white space characters from the beginning and end of this instance. So what for the middle ? .NET Framework Class Library
1
2913
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of fewer than 80 characters. It then prompts the user to type the search-substring of 10 or fewer characters. Finally, it prompts the user to type the replace-substring of 10 or fewer characters. The second function call is the search-and-replace...
8
80300
by: Johny | last post by:
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s,'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why? Thanks
8
22125
by: Warren Moxley | last post by:
Hi there, i've been searching for a C String search and replace function. I need to find all occurrences of " " in a char* array, and replace them with another char, I know how to do this in managed environments, but i'm still learning C, does anyone have a quicky function handy? or point me in the right direction on how to acccomplish this? your help is greatly appreciated. Also a note, this is running on a embedded device, so i can't...
10
18638
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The problem is I don't know the character to replace, just must replace the character at a known...
11
1932
by: windandwaves | last post by:
Hi Folk I need to write a tricky replacement function. C = replace A with B in C C = replace D with E in C examples of A could be "a cat climbs a tree", examples of B could be
5
1545
by: MLH | last post by:
I have memo field containing imported data. In it are numerous Chr(13) & Chr(13) & Chr$(10) occurrences. How do I rid my table of all occurrences of this 3-character string ? Generally there is only a single occurrence in the memo field of any record.
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.