java 如何使用java查找包含任何字母或数字字符的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6596078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:33:28  来源:igfitidea点击:

how to find String contain any alphabetic or numeric characters using java?

java

提问by Sameek Mishra

Possible Duplicate:
Simple way to determine if string is only characters, or to check if string contains any numbers in Java

可能的重复:
在 Java 中确定字符串是否只是字符或检查字符串是否包含任何数字的简单方法

I want to find if a given String has any alphabetic or numeric characters. How would I do this using java?

我想查找给定的字符串是否有任何字母或数字字符。我将如何使用 java 做到这一点?

Thanks

谢谢

回答by SteeveDroz

String s = "%$a*";
Pattern p = Pattern.compile("[a-zA-Z0-9]");
Matcher m = p.matcher(s);
if (m.find())
  System.out.println("The string \"" + s + "\" contains alphanumerical characters.");

回答by mre

There are two convenience methods in the Characterclass that would help you

Character课程中有两个方便的方法可以帮助您

All that's required is transforming a Stringto a character array, and then stepping through the array.

所需要做的就是将 aString转换为字符数组,然后逐步遍历该数组。