A
Andrew Maclean
I guess this problem can be distilled down to: How do I search through a
string, find the first matching substring, replace it, and continue through
the string doing this. Can replace_if() be used to do this?
Here is a concrete example:
If I have a string with sequences of CRLF and possibly just CR's, is there a
simple way of replacing the CRLF characters with a LF?
If you look at the function below is it possible to use a replace_if to do
this?
void CGeneralMessageDlg::ToLF( std::string & s)
{
char CR = '\r';
char LF = '\n';
std::string CRLF = "\r\n";
// Problem: how can I replace multiple CRLF's in a string with just one LF?
// This is my naive attempt, which doesn't work:
//
std::replace_if(s.begin(),s.end(),std::bind2nd(std::equal_to<std::string>(),
CRLF),LF);
// Replace any CR's with a LF.
std::replace_if(s.begin(),s.end(),std::bind2nd(std::equal_to<char>(),CR),LF)
;
}
Any comments/help would be appreciated.
Thankyou
Andrew
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
string, find the first matching substring, replace it, and continue through
the string doing this. Can replace_if() be used to do this?
Here is a concrete example:
If I have a string with sequences of CRLF and possibly just CR's, is there a
simple way of replacing the CRLF characters with a LF?
If you look at the function below is it possible to use a replace_if to do
this?
void CGeneralMessageDlg::ToLF( std::string & s)
{
char CR = '\r';
char LF = '\n';
std::string CRLF = "\r\n";
// Problem: how can I replace multiple CRLF's in a string with just one LF?
// This is my naive attempt, which doesn't work:
//
std::replace_if(s.begin(),s.end(),std::bind2nd(std::equal_to<std::string>(),
CRLF),LF);
// Replace any CR's with a LF.
std::replace_if(s.begin(),s.end(),std::bind2nd(std::equal_to<char>(),CR),LF)
;
}
Any comments/help would be appreciated.
Thankyou
Andrew
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]