Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STL replace

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I've been trying quite a few combinations on this one but this is the only one that works. Basically what I'm trying to do is replace "\r\n" with 2 spaces. The full code is given below. Is there a way of coding the section in blue in one line?
Code:
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
                                                                                
const char EOL[] = "\r\n";
                                                                                
int main ()
{
   ostringstream msg;
   string logger;
   int aaa = 1, bbb = 99, ccc = 1234;
                                                                                
   msg << "Param 1 " << aaa << EOL
       << "Param 2 " << bbb << EOL
       << "Param 3 " << ccc;
                                                                                
   logger = msg.str ();
   cout << "Screen output " << logger.c_str() << endl;
                                                                               [COLOR=BLUE]
   replace (logger.begin(), logger.end (), '\r', ' ');
   replace (logger.begin(), logger.end (), '\n', ' ');
                                                                               [/COLOR]
   cout << "log output " << logger.c_str() << endl;
   return 0;
}
 
I can't find a way to do it in one line, other than writing a function:
Code:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

const string EOL( "\r\n" );

void
ReplaceAll(  string&  str,
	   const string&  oldStr,
	   const string&  newStr )
{
	string::size_type pos		= 0;
	string::size_type oldSize	= oldStr.size();

	while ( (pos = str.find( oldStr, pos )) != string::npos )
	{
		str.replace( pos, oldSize, newStr );
	}
}


int main ()
{
   ostringstream msg;
   string logger;
   int aaa = 1, bbb = 99, ccc = 1234;

   msg << "Param 1 " << aaa << EOL
       << "Param 2 " << bbb << EOL
       << "Param 3 " << ccc;

   logger = msg.str ();
   cout << "Screen output " << logger.c_str() << endl;

   ReplaceAll( logger, EOL, "  " );

   cout << "log output " << logger.c_str() << endl;
   return 0;
}
BTW, the code you had won't do exactly what you said you want. It will replace all '\r' with ' ' and all '\n' with ' ', but they don't have to be "\r\n" to be replaced.
 
I know - but the way I've formed msg ensures that. We just have this thing that if we're displaying on the console, the users prefer everything on separate lines but if we're logging it , they want everything on one line.

This seems to be the easiest and most efficient approach. Other things that have been tried are creating two ostringstreams, one for screen and the other for logging and having a common routine writing to both of them or even having a template function to do the writing.

We've now got another requirement to dump it in csv format that gets exported out to a Windows system. Even more replacements.

 
well, I have an STL one-liner, but you probably aren't going to like it:

Code:
replace_if(logger.begin(),
           logger.end(),
           compose2(logical_or<bool>(),
                    bind2nd(equal_to<char>(), '\r'),
                    bind2nd(equal_to<char>(), '\n')),
           ' ');

--
 
I've never used compose2, logical_or or bind2nd before. Learn something new everyday ;)
 
IMHO, cpjust's solution in one line:
Code:
for (size_t i;(i=s.find("\r\n")) != string::npos; s.replace(i,2,"  "));
or better:
Code:
inline void clreols(string& s)
{for (size_t i;(i=s.find("\r\n")) != string::npos; s.replace(i,2,"  "));}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top