c++ - 删除 '\n.\n' C++

标签 c++ string

如果我有这样的字符串

"I am not here... \n..Hello\n.\n....Whats happening"

我想这样替换上面的字符串:

"I am not here... \n..Hello\n. \n....Whats happening"
                              ^ Space added

只是我在做什么的一些背景知识。我在 C++ 中使用 sendmail,\n.\nEnd Of Message 等同于 sendmail。我刚刚创建了一个使用 sendmail 发送邮件的类。但很明显,如果外部用户向 sendmail 发出该命令,我希望将其删除。这是我的消息功能,以防万一。:

//Operator to add to the message
void operator<<(string imessage){
    if (imessage != ""){ message += imessage; } 
}

我该怎么做。提前致谢:D

最佳答案

这是我的最后一个版本:)
此代码处理@Greg Hewgill 提到的案例

string& format_text(string& str)
{
    const string::size_type dot_offset = 2;
    string::size_type found_at_start = str.find("\n.\n"),
                    found_at = str.find("\n.\n");

    if(found_at_start != string::npos)
        str.insert(0, " ");

    while(found_at != string::npos)
    {
        str.insert(found_at+dot_offset+1, " ");
        found_at = str.find("\n.\n", found_at+1);
    }
    return str;
}

int main()
{
    string text = ".\nn\n.\nn";
    std::cout << format_text(text);
}

关于c++ - 删除 '\n.\n' C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2174273/

相关文章:

c++ - 为什么 const 成员函数可以修改静态数据成员?

c++ - 调用 gluPerspective 和 gluLookAt 给我一个 undefined reference ?

c++ - qt在编译时生成包含git hash的头文件并使用文件强制重建

string - perl:显示原始字符串,包括特殊字符

c++ - 关于在 C++ 中通过 ASCII 将文本转换为十六进制

c++ - 无法链接 BEGIN_MESSAGE_MAP 中的 afx_msg

c++ - 使用虚空函数但没有编译错误的类中的运行时错误

java - 在 JAVA 中替代 PHP 的关联数组

ios - 将 [String] 存储在 NSUserDefaults 中

java - String.replaceAll() 不适用于某些字符串