Is there a replace function, what I can use on strings, to replace certain characters. For example, replace all "_" with " ".
Printable View
Is there a replace function, what I can use on strings, to replace certain characters. For example, replace all "_" with " ".
Yes. You can implement it yourself. Just loop through all the characters of a string and whenever one is of the type you want to replace, assign it the replacement value.
If you are using std::strings, there are a bunch of replace methods. I don't have time to find a link or post all of them but here's two:
A google search might turn more up.Code:string& string::replace (size_type idx, size_type len, size_type num, char c)
string& string::replace (iterator beg, iterator end, size_type num, char c)
//replace at most len characters starting at idx or in the range [beg, end)
//with num occurrences of character c
here are two examples
Code:EXAMPLE #1:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string search ("Im going to change the spaces to !");
string::size_type pos = 0;
while( (pos = search.find(" ", pos+1)) != string::npos )
{
search.replace( pos , 1 , "!" );
}
cout << search << endl;
cin.get();
return 0;
}
EXAMPLE #2:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string search ("Im going to change the spaces to !");
for( int i = 0; i != search.size(); i++ )
{
if( search[ i ] == ' ' )
{
search[ i ] = '!';
}
}
cout << search << endl;
cin.get();
}
Ah sorry, I was in a hurry before and I guess I didn't read your post carefully. I don't think there's a replace function that does specifically what you want, but I guess ILV's solution would work :)
The replace algorithm will do what you want in one line:Code:std::replace(str.begin(), str.end(), '_', ' ');
and incase your not sure what you need / how to implement
here is an example of it
Code:#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string search ("Im_going_to_change_the_underlines_to_spaces");
replace( search.begin(), search.end(), '_' , ' ' );
cout << search << endl;
cin.get();
return 0;
}
In ILV's second example, you could also make use of std::string::iterators.
yea i was going to use iterators, but i figured id leave one that
was more easily understood, if he hasnt fimilarized himself
with to many aspect of the language.
here an example of that to
Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
string search ("Im_going_to_change_the_underlines_to_spaces");
string::iterator underlinefind;
for( underlinefind = search.begin(); underlinefind != search.end(); underlinefind++ )
{
if( *(underlinefind) == '_' )
{
*(underlinefind) = ' ';
}
}
cout << search << endl;
cin.get();
}
but if i want to replace "_*_" to " "
you seriously need to try and write your own, i gave you 3
wonderful examples, but because im not in my assholish mood
yet.......
this one doesnt do exactly what you want, but a few change
on your part and you will have what you want.
Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
string search ("Im_going_to_change_the_*_underlines_to_spaces");
string::size_type pos = -1;
while( ( pos = search.find("_*_", pos+1) ) != string::npos )
{
search.erase( (search.begin() + pos), (search.begin() + (pos + 3)) );
search.insert( pos, "_" );
}
cout << search << endl;
cin.get();
return 0;
}