2010-07-09 41 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