-
Mega String Manipulation
How would I be able to manipulate a string so that
1. I can change character x to another character
2. I can add a certain character in between two characters
3. I can change all the characters of a certain type to another character
Code would be useful please...
Thanks,
Bumfluff
-
Or even easier...
How do I assign the contents of a string to a character array which has the same length as the string.
-
http://www.cppreference.com/cppstring/index.html
Look at the functions in there. They should answer your first few questions.
Code:
std::string str1 = "Hello World!";
char str2[str1.size() + 1];
strcpy(str2, str1.c_str());
-
Well I worked out an easier way to do most of it with an array...but I could only get my array = to the string with extra strange characters on the end.
Will try your code...
-
-
1. Make sure that string is not const and call the assignment operator after you index the character. C-strings are easy, just use memset().
PHP Code:
std::string brand("Jello Kitty");
brand[0] = 'H'; // "Hello Kitty"
char cstrbrand[13] = brand.c_str();
memset(cstrbrand, 'J', 1); // "Jello Kitty" again
2. Use insert() on the string object.
3. I'm not sure I understand, but I suppose you could just use the replace function if you know where to start. If not you have to find() it first.
PHP Code:
std::string msg("My name is George W. Bush and I approve this message!");
msg.replace(11, 14, "citizen");
Crap, a bit slow huh? At least the code is pretty.