Raviprakash
Raviprakash

Reputation: 2440

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 perform 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.

Upvotes: 1

Views: 3474

Answers (2)

Crazyshezy
Crazyshezy

Reputation: 1620

you can use regular expressions to pattern match. library regexp.h is to be included

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

Upvotes: -1

Georg Fritzsche
Georg Fritzsche

Reputation: 99122

Use find_first_not_of():

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

Upvotes: 9

Related Questions