如何检查字符串中是否只有选定的字符?
检查字符串是否仅包含以下字符的最佳和最简单的方法是什么:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
我想要一个像这样的伪代码的示例:
//If String contains other characters
else
//if string contains only those letters
请并谢谢:)
What's the best and easiest way to check if a string only contains the following characters:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
I want like an example like this pseudo-code:
//If String contains other characters
else
//if string contains only those letters
Please and thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于该特定类别的字符串,请使用正则表达式“\w+”。
请注意,我使用 Pattern 对象来编译正则表达式一次,这样就不必再次编译它,如果您要多次或在循环中进行此检查,这可能会很好。根据java文档...
For that particular class of String use the regular expression "\w+".
Note that I use the Pattern object to compile the regex once so that it never has to be compiled again which may be nice if you are doing this check in a-lot or in a loop. As per the java docs...
轮到我了:
上面搜索单个不匹配或空字符串。
并根据 JavaDoc for Pattern:
My turn:
Above searches for single not matching or empty string.
And according to JavaDoc for Pattern: