string contains valid characters
I am writing a method whose signature is
bool isValidString(std::string value)
Inside this method I want to search all the characters in value
are belongs to a set of characters which is a constant string
const std::string ValidCharacters("abcd")
To perfo开发者_Go百科rm this search I take one character from value
and search in ValidCharacters
,if this check fails then it is invalid string is there any other alternative method in STL library to do this check.
Use find_first_not_of()
:
bool isValidString(const std::string& s) {
return std::string::npos == s.find_first_not_of("abcd");
}
you can use regular expressions to pattern match. library regexp.h is to be included
http://www.digitalmars.com/rtl/regexp.html
精彩评论