我想知道如何将一条线分成只包含数字和字母的字符串(没有标点符号)。我在网上看过这样做的公式,但是省略了如何省略标点符号的说明。
答案 0 :(得分:2)
您可以使用<cctype>
中的功能来测试您的角色。在您的情况下,您可能希望使用isalnum
。
答案 1 :(得分:1)
这就是擦除删除习惯用法派上用场的地方。编辑:lambda不必要。
#include <iostream>
#include <algorithm>
#include <cctype>
int main()
{
std::string s = "!%@T%abc";
s.erase(std::remove_if(s.begin(), s.end(), ispunct), s.end());
std::cout << s;
}
答案 2 :(得分:0)
细节根据您想要实现的内容而有所不同,但让我们假设您需要一组字符而其他所有字符都是不需要的,并且输出将是输入中所有连续的有用字符组。然后伪代码将是:
while input not empty
remove all unwanted characters from from of input string
find first unwanted character in input string
if not found
last output string is remains of input string
empty out input string
else
next output string is all chars before unwanted char
remove all the wanted chars we found from the input string
end
对不起伪代码,但这是我能做的最好的事情,而不知道你正在使用什么字符串类。