字符串包含有效字符

发布于 2024-09-09 06:09:19 字数 341 浏览 1 评论 0原文

我正在编写一个方法,其签名是

bool isValidString(std::string value)

在该方法内我想搜索 value 中的所有字符都属于一组字符,该字符是常量字符串

const std::string ValidCharacters("abcd")

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧瑾黎汐 2024-09-16 06:09:19

使用find_first_not_of()

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

Use find_first_not_of():

bool isValidString(const std::string& s) {
    return std::string::npos == s.find_first_not_of("abcd");
}
很酷不放纵 2024-09-16 06:09:19

您可以使用正则表达式进行模式匹配。
将包含库 regexp.h

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文