更新时间:2023-02-22 15:45:17
您可以使用正则表达式进行检查.
You can check this with a regex.
假设(仅数字值):
String a = "493284835";
a.matches("^[0-9]+$"); // returns true
假设(仅字母数字值):
Suppose that (alphanumeric values only):
String a = "dfdf4932fef84835fea";
a.matches("^([A-Za-z]|[0-9])+$"); // returns true
正如Pangea在评论区所说:
As Pangea said in the comments area :
如果性能至关重要,则***编译正则表达式.参见下面的示例:
If the performance are critical, it's preferrable to compile the regex. See below for an example :
String a = "dfdf4932fef84835fea";
Pattern pattern = Pattern.compile("^([A-Za-z]|[0-9])+$");
Matcher matcher = pattern.matcher(a);
if (matcher.find()) {
// it's ok
}