STL string

G

Gurikar

Hello,
I have a string called "John mathew dsouza". Now i want replace all
spaces in this string to '-', i mean i want the output string to be
like this: "John-mathew-dsouza". Any shortcut methods are there using
C++ STL string(i mean any readymade API's in C++ string).

Regards
 
V

Victor Bazarov

Gurikar said:
I have a string called "John mathew dsouza". Now i want replace all
spaces in this string to '-', i mean i want the output string to be
like this: "John-mathew-dsouza". Any shortcut methods are there using
C++ STL string(i mean any readymade API's in C++ string).

A simple loop with a single 'if' inside should do.
 
W

Winbatch

Victor Bazarov said:
A simple loop with a single 'if' inside should do.
Not sure it's the most efficient, etc, but here's how I wrote it:
bool StringUtils::replaceString( const string &original, string
&finalString, const string & sourceString, const string & targetString )
{
finalString = original;
int len = sourceString.size();
bool changesMade=false;

int pos=0;
while( pos != -1)
{
pos = finalString.find( sourceString, pos );

if ( pos != -1 )
{
finalString.erase( pos, len );
if (targetString.size() > 0 )
finalString.insert( pos, targetString );
changesMade=true;
}

}
return changesMade;
}
 
K

Ken Human

Winbatch said:
Not sure it's the most efficient, etc, but here's how I wrote it:
bool StringUtils::replaceString( const string &original, string
&finalString, const string & sourceString, const string & targetString )
{
finalString = original;
int len = sourceString.size();
bool changesMade=false;

int pos=0;
while( pos != -1)
{
pos = finalString.find( sourceString, pos );

if ( pos != -1 )
{
finalString.erase( pos, len );
if (targetString.size() > 0 )
finalString.insert( pos, targetString );
changesMade=true;
}

}
return changesMade;
}

Seems a little more than what OP asked for. How does the following work?

ken@ken-wn0vf73qmks ~/c
$ cat strRep.cpp
#include <string>
#include <iostream>
using namespace std;

int main() {
string str = "Ken Human is replacing spaces with dashes.";
for(unsigned int i = 0; i < str.size(); i++)
if(str == ' ') str = '-';
cout << str << endl;
return 0;
}

ken@ken-wn0vf73qmks ~/c
$ ./strRep
Ken-Human-is-replacing-spaces-with-dashes.
 
T

Thierry Miceli

This is less straightforward than a simple loop but if you want an all STL
solution try using replace_if.

#include <string>
#include <algorithm>
#include <functional>
#include <iostream>

int main()
{
std::string str = "John mathew dsouza";
std::replace_if(str.begin(), str.end(), std::bind2nd(std::equal_to<char>(),'
'), '-');

std::cout << str;
return 0;
}

Regards,

Thierry Miceli
www.ideat-solutions.com
 
P

Phil Slater

.... or just the simple std::replace in section 25.2.4 of the language
standard - it saves doing the bind2nd.

HTH
 
C

Chris \( Val \)

| This is less straightforward than a simple loop but if you want an all STL
| solution try using replace_if.
|
| #include <string>
| #include <algorithm>
| #include <functional>
| #include <iostream>
|
| int main()
| {
| std::string str = "John mathew dsouza";
| std::replace_if(str.begin(), str.end(), std::bind2nd(std::equal_to<char>(),'
| '), '-');

[snip]

Prefer:

std::replace( str.begin(), str.end(), ' ', '-' );

:)

Cheers,
Chris Val
 
T

Thierry Miceli

... or just the simple std::replace in section 25.2.4 of the language
standard - it saves doing the bind2nd.
Yes! Forgot this version of replace did exist
 
B

Billy Patton

Hello,
I have a string called "John mathew dsouza". Now i want replace all
spaces in this string to '-', i mean i want the output string to be
like this: "John-mathew-dsouza". Any shortcut methods are there using
C++ STL string(i mean any readymade API's in C++ string).

Regards

Here's one I use constantly. I got the results from this group.

void StrSub(string& cp,string sub_this,string for_this,int num_times)
{
int i,loc;
if (cp.empty())
{
cp = sub_this;
return;
}
for (i = 0; i != num_times; i++)
{
loc = cp.find(for_this,0);
if (loc >= 0) cp.replace(loc,for_this.length(),sub_this);
else return;
}
}

I have this to be able to replace a string N times.
Defailt is -1 so that it will replace all.
This is to give me perl type subs'
s/aaa/bbb/g to replace all
s/aaa/bbb/ to replace just the first.



___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 

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
474,293
Messages
2,571,500
Members
48,187
Latest member
ashish_758

Latest Threads

Top