c++ - 字符串包含有效字符

标签 c++ stl stdstring

我正在写一个方法,其签名是

bool isValidString(std::string value)

在这个方法中,我想搜索value中的所有字符都属于一组字符,这是一个常量字符串

const std::string ValidCharacters("abcd")

为了执行此搜索,我从 value 中取出一个字符并在 ValidCharacters 中搜索,如果此检查失败则它是无效字符串 STL 库中是否还有其他替代方法做这个检查。

最佳答案

使用find_first_not_of() :

bool isValidString(const std::string& s) {
    return std::string::npos == s.find_first_not_of("abcd");
}

关于c++ - 字符串包含有效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211678/

相关文章:

c++ - std::map 设计:为什么 map 接受比较器作为模板参数

c++ - 当字符串包含 %2C 时,c++ 中的 std::string 不完整

c++ - 检查字符串是否在以字符串 vector 为值的映射的值中

c++ - C++模板打包参数

c++ - GNU 链接器 : Adapt to change of name mangling algorithm

c++ - QTextBrowser 换行后的行间距

c++ - 如何序列化并通过网络发送 std::list?

c++ - std::transform 中的一元运算符

c++ - 包含传递给 lua 的 std::string 的结构

c++ - reinterpret_cast<unsigned long> 无效转换