String manipulation

Started by
5 comments, last by Ruudje 20 years, 6 months ago
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?
Advertisement
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).
std::string mystring = "This is my very long string that i hate";//now to cut off some of the stringstd::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 longermy_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

Well unfortunately its an AnsiString
Copy the string starting at the position of ''\0'', then add one at the end of that string.

use strcat() ?
daerid@gmail.com
And don''t forget to add another ''\0'' after the other thing has been appended.

This topic is closed to new replies.

Advertisement