Checks if the string contains only ASCII printable characters in Java
Description
The following code shows how to checks if the string contains only ASCII printable characters.
Example
/*from ww w. ja v a 2s . c o m*/
public class Main {
public static boolean isAsciiPrintable(String str) {
if (str == null) {
return false;
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
if (isAsciiPrintable(str.charAt(i)) == false) {
return false;
}
}
return true;
}
public static boolean isAsciiPrintable(char ch) {
return ch >= 32 && ch < 127;
}
public static void main(String[] argv){
System.out.println(isAsciiPrintable("java2s.com") );
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »