【问题标题】:How to Find and Replace the Enter character?如何查找和替换 Enter 字符?
【发布时间】:2010-04-22 03:44:29
【问题描述】:

如何查找和替换文本文件中的“Enter”字符?这是我的代码:

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++; }

【问题讨论】:

  • 你有什么问题?
  • 乔治,它不知何故没有检测到 Enter 字符。

标签: visual-c++ char


【解决方案1】:

在文本文件中搜索 \r\n 用于 Windows 格式,\n 用于 Linux/Unix 格式,\r 用于 Mac 格式的文本文件。

【讨论】:

    【解决方案2】:

    输入字符是“\n”,但在 Windows 中是“\r\n”,旧 Mac 是“\r”。 对于混合换行符:正则表达式 /(?

    【讨论】:

    • 正则表达式? OP 在哪里要求的?
    • @George,您可以使用正则表达式查找和替换。 OP 确实要求查找和替换。
    【解决方案3】:

    取决于你的目标是什么。

    如果您希望所有换行符看起来都一样,you'd better be thorough。根据链接的文章,至少有五种模式需要寻找:

    • Windows (CR LF)
    • Mac (CR)
    • Unix (LF)
    • Unicode 行分隔符
    • Unicode 段落分隔符

    我认为寻找字节模式(将其视为二进制文件而不是文本文件)将是可行的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多