我想在C++中检查一个char
(string.at(i)
)是否为空格。如何轻松实现?
我有这个代码示例,我想更改_____
为其他内容,但不知道应该填什么。我已经尝试过' '
,但没有成功。
for(int i = 0; i < string.length(); i++)
{
if(string.at(i) == _________)
{
//do something
}
}
我想在C++中检查一个char
(string.at(i)
)是否为空格。如何轻松实现?
我有这个代码示例,我想更改_____
为其他内容,但不知道应该填什么。我已经尝试过' '
,但没有成功。
for(int i = 0; i < string.length(); i++)
{
if(string.at(i) == _________)
{
//do something
}
}
#include <cctype>
if (isspace(string.at(i)))
==
[something],您需要使用if (isspace(string.at(i))
(或者您可能更喜欢使用std::isspace
)。std::remove_copy_if(s.begin(), s.end(), std::back_inserter(new_string), isspace);
不后悔的C程序员转向C++时,会半自动地使用:
#include <cctype>
if (std::isspace(string.at(i)))
...
即使对于C++程序员来说,这也很可能是正确的。
string[i] == ' '
应该可以工作,如果你想要非常简单的检查。 - Mateen Ulhaqat
,因为您知道索引是有效的。 - GManNickG\t
、\r
和\n
,以及其他一些控制字符。 - dan04