开发者

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

开发者 https://www.devze.com 2023-03-18 04:09 出处:网络
This question already has answers here: 开发者_Python百科 Closed 11 years ago. Possible Duplicate: Simple way to determine if string is only characters, or to check if string contains any numbe
This question already has answers here: 开发者_Python百科 Closed 11 years ago.

Possible Duplicate:

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

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

Thanks


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.");


Take a look at this: http://www.regular-expressions.info/java.html


There are two convenience methods in the Character class that would help you

  • isLetter(...)
  • isDigit(...)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消