Replacing Strings in C++
Posted: Mon Apr 23, 2007 2:24 am
I'm using the string class in a C++ program I'm writing to assist me in diagnosing network issues.
The problem is, I need to be able to handle newlines written into the console as '\n' (ie. '\\', 'n'). I need to be able to replace the string "\n" (which is two bytes) to '\n' (the one byte version), and make sure no remnants are left over.
I'm using VC++ 6.
Edit: never mind, I got it:
The problem is, I need to be able to handle newlines written into the console as '\n' (ie. '\\', 'n'). I need to be able to replace the string "\n" (which is two bytes) to '\n' (the one byte version), and make sure no remnants are left over.
I'm using VC++ 6.
Edit: never mind, I got it:
Code: Select all
// search for newlines
int lastf = 0;
for( i = 0; i < dat.length(); i++ )
{
if( lastf != 0 )
{
lastf = dat.find( "\\n", lastf + 1 );
}
else
{
lastf = dat.find( "\\n", lastf );
}
if( lastf == -1 )
break;
else
{
dat.replace( lastf, 2, "\n" );
}
}