字符串包含有效字符

时间:2010-07-09 10:19:14

标签: c++ stl stdstring

我正在编写一个签名为

的方法
bool isValidString(std::string value)

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

const std::string ValidCharacters("abcd")

要执行此搜索,我从value中取一个字符并在ValidCharacters中搜索,如果此检查失败,那么它是无效字符串是否在STL库中有任何其他替代方法来执行此检查。< / p>

2 个答案:

答案 0 :(得分:9)

使用find_first_not_of()

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

答案 1 :(得分:-1)

您可以使用正则表达式进行模式匹配。 库regexp.h将被包含在内

http://www.digitalmars.com/rtl/regexp.html

相关问题