views:

44

answers:

3

How to Find and Replace the 'Enter' characters in the text file? Here is my code:

string searchString( "\r" );   // <------- how to look for ENTER chars?
string replaceString( "XXXX" );

assert( searchString != replaceString );

string::size_type pos = 0, pos3 =0;

while ( (pos = test.find(searchString, pos)) != string::npos ) {
    test.replace( pos, searchString.size(), replaceString );
            pos++; }
+1  A: 

Search \r\n for Windows format, \n for Linux/Unix format, \r for Mac format in text files.

S.Mark
+1  A: 

Depends on what your goals are.

If you want all newline characters to look the same, you'd better be thorough. According to the linked article, there are at least five patterns to look for:

  • Windows (CR LF)
  • Mac (CR)
  • Unix (LF)
  • Unicode Line Separator
  • Unicode Paragraph Separator

I'd think looking for byte patterns (by treating it as a binary file rather than a text file) would be the way to go.

John at CashCommons
+1  A: 

The enter character is "\n", but in Windows, "\r\n", old Mac "\r". For mixed newlines: regexp /(?<[^\r\n])(?:\r|\n|\r\n)(?=[^\r\n])/

SHiNKiROU
RegEx? Where did the OP ask for that?
George Edison
@George, you can use RegEx to find and replace. OP did ask for find and replace.
John at CashCommons