2010-07-09 82 views
1

我写的签名是字符串中包含有效的字符

bool isValidString(std::string value) 

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

const std::string ValidCharacters("abcd") 

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

回答

9

使用find_first_not_of()

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

你可以使用正则表达式模式匹配。 库regexp.h将被包括

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

+0

那d,而不是C++。 – Puppy 2010-07-09 10:28:53

+0

你甚至去链接,并阅读第一行 – Crazyshezy 2010-07-09 10:29:57

+0

@Dead:虽然它的矫枉过正在这里:*“RegExp是一个C++类来处理正则表达式。”* – 2010-07-09 10:30:35