473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 25489
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*********************@o80g2000hse.googlegro ups.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::endl;
// out: replace =+Hello+, +World+

--
Bob R
POVrookie
Aug 30 '07 #5
On 2007-08-30 08:09, di*****@gmail.com 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
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...
3
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...
3
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...
1
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...
8
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?...
8
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...
10
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...
11
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.