placing new characters at the end of string

F

friend.blah

i have a string say

string crap = "abc:abd:aass";

at the end of my string i want to place new characters like "xx"
so the final string looks like

string crap = "abc:abd:aassxx";

how to resolve this issue...

i could able replace colons :)) with other characters but i am failing
when i wanna place new characters at the end of string.


thanks for any help
 
I

Ian Collins

i have a string say

string crap = "abc:abd:aass";
Assuming this is std::string...
at the end of my string i want to place new characters like "xx"
so the final string looks like

string crap = "abc:abd:aassxx";

how to resolve this issue...
By adding them?

What have you tried?
 
F

friend.blah

Assuming this is std::string...




By adding them?

What have you tried?

i am sorry... i got it....
it will work if do it like this

crap.replace(crap.length(),1,"xx");

anyway thanks...
 
P

Pascal J. Bourguignon

i have a string say

string crap = "abc:abd:aass";

at the end of my string i want to place new characters like "xx"
so the final string looks like

string crap = "abc:abd:aassxx";

how to resolve this issue...

i could able replace colons :)) with other characters but i am failing
when i wanna place new characters at the end of string.


thanks for any help


#include <string>
#include <iostream>
using namespace std;

int main(void){
string a="abc";
a.push_back('.');
a+="def";
a=a+".ghi";
cout<<a<<endl;
return(0);
}

/*
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Tue May 6 10:44:39

g++ -o a a.c++ && ./a
abc.def.ghi

Compilation finished at Tue May 6 10:44:39
*/


Have a look at:
http://www.cplusplus.com/reference/string/
 
J

Jim Langston

i have a string say

string crap = "abc:abd:aass";

at the end of my string i want to place new characters like "xx"
so the final string looks like

string crap = "abc:abd:aassxx";

how to resolve this issue...

i could able replace colons :)) with other characters but i am failing
when i wanna place new characters at the end of string.


thanks for any help

std::string crap = "abc:abd:aass";

1. crap += "xx";
2. crap = crap + "xx";
3. crap += 'x'; crap += 'x';
4. crap.push_back('x'); crap.push_back('x');
5....
 
A

Andrew Koenig

i am sorry... i got it....
it will work if do it like this
crap.replace(crap.length(),1,"xx");

Yes, it will. However, it would be more honest to write it this way:

crap.replace(crap.length(),0,"xx");

and even easier to write it this way:

crap += "xx";

as others have already pointed out.
 

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
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top