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 8 8972
Gurikar wrote: 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.
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:v4********************@comcast.com... Gurikar wrote: 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.
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;
}
Winbatch wrote: "Victor Bazarov" <v.********@comAcast.net> wrote in message news:v4********************@comcast.com...
Gurikar wrote:
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.
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[i] == ' ') str[i] = '-';
cout << str << endl;
return 0;
}
ken@ken-wn0vf73qmks ~/c
$ ./strRep
Ken-Human-is-replacing-spaces-with-dashes.
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
.... or just the simple std::replace in section 25.2.4 of the language
standard - it saves doing the bind2nd.
HTH
"Thierry Miceli" <tm*****@gmail.com> wrote in message
news:17*********************@news1.sympatico.ca... 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
"Thierry Miceli" <tm*****@gmail.com> wrote in message
news:17*********************@news1.sympatico.ca...
| 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 ... 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
On Mon, 25 Apr 2005, Gurikar wrote: 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, b-******@ti.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Krakatioison |
last post by:
My sites navigation is like this:
http://www.newsbackup.com/index.php?n=000000000040900000
, depending on the variable "n" (which is always a number), it will take me
anywhere on the site......
|
by: Stu Cazzo |
last post by:
I have the following:
String myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it...
|
by: John F Dutcher |
last post by:
I use code like the following to retrieve fields from a form:
recd =
recd.append(string.ljust(form.getfirst("lname",' '),15))
recd.append(string.ljust(form.getfirst("fname",' '),15))
etc.,...
|
by: Angus Leeming |
last post by:
Hello,
Could someone explain to me why the Standard conveners chose to typedef
std::string rather than derive it from std::basic_string<char, ...>?
The result of course is that it is...
|
by: Andrew |
last post by:
I have written two classes : a String Class based on the book " C++ in
21 days " and a GenericIpClass listed below :
file GenericStringClass.h
// Generic String class
|
by: zoro |
last post by:
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return...
|
by: Badass Scotsman |
last post by:
Hello,
Using VB and ASP,NET I would like to be able to search a STRING for a
smaller STRING within, based on the characters which appear before and
after.
For example:
String1 = " That was...
|
by: morleyc |
last post by:
Hi, i would like to remove a number of characters from my string (\t
\r \n which are throughout the string), i know regex can do this but i
have no idea how. Any pointers much appreciated.
Chris
|
by: ramu |
last post by:
Hi,
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove...
|
by: drjay1627 |
last post by:
hello,
This is my 1st post here!
*welcome drjay*
Thanks! I look answering questions and getting answers to other!
Now that we got that out of the way. I'm trying to read in a string and...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
| |