在C ++中摆脱CString中的换行符

时间:2012-07-31 10:21:56

标签: c++ xml string newline cstring

我有一个最初是CString的html / xml文档,我想摆脱所有换行符,基本上把所有内容放到一行。我已经尝试将其转换为std :: String并使用:

#include <algorithm>
#include <string>

str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());

但它不起作用。

3 个答案:

答案 0 :(得分:3)

为了阻止您的文本块看起来很奇怪,您需要用空格替换换行符。确保替换换行符('\ n')和回车符('\ r')字符。

CString str = "Line 1 Windows Style\r\n Line 2 Unix Style\n Line 3";
str.Replace('\r', " ");
str.Replace('\n', " ");
str.Replace("  ", " ");

答案 1 :(得分:2)

您只需要使用方法删除

CString str = _T("Test newline \nremove"), str2;
str.Remove('\n');

答案 2 :(得分:0)

怎么样?

str.Replace("\n", "");

记录here