[英]Check if string.at(i) is a whitespace C++
我想檢查一下char
( string.at(i)
)是否是C ++中的空格。 我怎么能這么容易做到?
我得到了這個代碼示例,我正在考慮用某些東西改變_____
,但不知道是什么。 我試過' '
,但那沒用。
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 ++程序員來說,它也很可能是正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.