如何检查字符串中是否只有选定的字符? [英] How to check if only chosen characters are in a string?

查看:143
本文介绍了如何检查字符串中是否只有选定的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查字符串是否只包含以下字符的最好和最简单的方法是什么:

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

请感谢:)

推荐答案

if (string.matches("^[a-zA-Z0-9_]+$") {
  // contains only listed chars
} else {
  // contains other chars
}

这篇关于如何检查字符串中是否只有选定的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆