For a non-regular expression approach, you can check Character.isWhitespace
for each character.
boolean containsWhitespace(String s) {
for (int i = 0; i < s.length(); ++i) {
if (Character.isWhitespace(s.charAt(i)) {
return true;
}
}
return false;
}
Which are the white spaces in Java?
The documentation specifies what Java considers to be whitespace:
public static boolean isWhitespace(char ch)
Determines if the specified character is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria:
- It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
- It is
'\u0009'
, HORIZONTAL TABULATION.- It is
'\u000A'
, LINE FEED.- It is
'\u000B'
, VERTICAL TABULATION.- It is
'\u000C'
, FORM FEED.- It is
'\u000D'
, CARRIAGE RETURN.- It is
'\u001C'
, FILE SEPARATOR.- It is
'\u001D'
, GROUP SEPARATOR.- It is
'\u001E'
, RECORD SEPARATOR.- It is
'\u001F'
, UNIT SEPARATOR.