To check if a Java String is not all whitespaces, you can use the trim()
method to remove any leading or trailing whitespaces from the string. Then you can check if the string is empty or null. Here's an example:
String str = " ";
if (str != null && !str.isEmpty()) {
System.out.println("The string is not all whitespaces.");
} else {
System.out.println("The string is all whitespaces.");
}
Alternatively, you can use the matches()
method of the String
class to check if the string matches the regular expression \S
(which matches any non-whitespace character). Here's an example:
String str = " ";
if (str.matches("\\S")) {
System.out.println("The string is not all whitespaces.");
} else {
System.out.println("The string is all whitespaces.");
}
In both cases, the output will be:
The string is not all whitespaces.
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云