开发者

How to Find and Replace the Enter character?

开发者 https://www.devze.com 2022-12-27 13:57 出处:网络
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?

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 != replaceSt开发者_JAVA百科ring );

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

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


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


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])/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消