开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜