Archived

This topic is now archived and is closed to further replies.

Ruudje

String manipulation

Recommended Posts

Hey all ... this has got me stumped. At some point I cut off a part of a string by placing a "\0" in it. The problem is though, that at a later point I need to add another string to this string, which isnt possible, as it will be added AFTER the "\0", so effectively the string doesnt change... How can I do this so that I DO get the new part attached to it?

Share this post


Link to post
Share on other sites
Assuming these are std::strings, use erase to cut the string instead.

mystring.erase(pos); 

Instead of
mystring[pos] = ''\0''; 


Then you can append a new string without a problem. The problem here is that a std::string is not a 0-delimited string by nature - you inserting ''\0'' in it doesn''t really change the string (it still has the same length).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

std::string mystring = "This is my very long string that i hate";

//now to cut off some of the string


std::string my_shorter_string = mystring.substr(0,26); //get 26 chars from the string (This is my very long string)


//You then decide you prefer the string longer

my_shorter_string += " that i LOVE!";

//you now have in my_shorter_string "This is my very long string that i LOVE!" .. or something close to that



Havnt tested this

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And don''t forget to add another ''\0'' after the other thing has been appended.

Share this post


Link to post
Share on other sites