[Q#6596078][A#6596139] how to find String contain any alphabetic or numeric characters using java?
https://stackoverflow.com/q/6596078
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
Answer
https://stackoverflow.com/a/6596139
APIzation
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.");
package com.stackoverflow.api;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Human6596139 {
public static boolean isStringAlphanumeric(String s) {
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."
);
return m.find();
}
}
package com.stackoverflow.api;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* how to find String contain any alphabetic or numeric characters using java?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/6596139">https://stackoverflow.com/a/6596139</a>
*/
public class APIzator6596139 {
public static void find(String s) throws Exception {
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."
);
}
}