从 C++ 中的字符串中删除 \r
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2528995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove \r from a string in C++
提问by Open the way
in a C++ program, there is a point when it reads a string like:
在 C++ 程序中,当它读取一个字符串时有一点:
"NONAME_1_1\r"
the \r
is causing me trouble. I guess it prints or adds something like "^M". Is it right? Anyway it casues me problem, and I want to get rid of it.
这\r
给我带来了麻烦。我猜它会打印或添加类似“^M”的内容。这样对吗?无论如何,它给我带来了问题,我想摆脱它。
I can not modify the input. I wonder how could I at this point, using C++, and in the easiest way, to remove \r
for this string.
我无法修改输入。我想知道此时如何使用 C++ 以最简单的方式删除\r
此字符串。
I know how to do it on bash but no clue on C++.
我知道如何在 bash 上做到这一点,但对 C++ 一无所知。
Thanks.
谢谢。
回答by CB Bailey
I'm assuming that by string, you mean std::string
.
我假设通过字符串,你的意思是std::string
.
If it's only the last character of the string that needs removing you can do:
如果它只是需要删除的字符串的最后一个字符,您可以执行以下操作:
mystring.pop_back();
mystring.pop_back();
mystring.erase(mystring.size() - 1);
Edit: pop_back()
is the next version of C++, sorry.
编辑:pop_back()
是 C++ 的下一个版本,抱歉。
With some checking:
通过一些检查:
if (!mystring.empty() && mystring[mystring.size() - 1] == '\r')
mystring.erase(mystring.size() - 1);
If you want to remove all \r
, you can use:
如果要删除 all \r
,可以使用:
mystring.erase( std::remove(mystring.begin(), mystring.end(), '\r'), mystring.end() );
回答by Marcelo Cantos
That depends on how you are holding it in memory:
这取决于您如何将其保存在内存中:
- If it's in a std::string, just check the last byte and remove it if it's a
'\r'
. - If it's in a const char*, you can use strncpy to copy the string into another char array, conditionally grabbing the last byte.
- If, by "I can not modify the input," you simply mean that you can't touch the source file, then you may have the option to read it into a char array and replace any trailing
'\r'
with'\0'
.
- 如果它在 std::string 中,只需检查最后一个字节,如果它是
'\r'
. - 如果它在 const char* 中,则可以使用 strncpy 将字符串复制到另一个 char 数组中,有条件地获取最后一个字节。
- 如果“我无法修改输入”只是意味着您无法访问源文件,那么您可以选择将其读入字符数组并将任何尾随替换
'\r'
为'\0'
.
回答by Stephen
This is a common problem when reading lines from files after moving them between unix and windows.
这是在 unix 和 windows 之间移动文件后从文件中读取行时的常见问题。
- Unix variants use "\n" (line feed) to terminate lines.
- Windows uses "\r\n" (carriage return, line feed) to terminate lines.
- Unix 变体使用“\n”(换行)来终止行。
- Windows 使用“\r\n”(回车、换行)来终止行。
You can run the "dos2unix" or "unix2dos" to convert lines in a file.
您可以运行“dos2unix”或“unix2dos”来转换文件中的行。
回答by Indranil
copy_if is useful if you want to leave the original string untouched and also if you want to want remove multiple characters like CR & LF in this example.
如果您想保持原始字符串不变,并且您想删除多个字符(如本例中的 CR 和 LF),则 copy_if 很有用。
const std::string input = "Hello\r\nWorld\r\n";
std::string output;
output.reserve(input.length());
std::copy_if(input.begin(), input.end(),
std::back_inserter(output),
[] (char c) { return c != '\r' && c != '\n'; });