What are kinds of whitespaces in Java? I need to check in my code if the text contains any whitespaces.
My code is:
if (text.contains(" ") || text.contains("\t") || text.contains("\r")
|| text.contains("\n"))
{
//code goes here
}
I already know about \n
,\t
,\r
and space
.
replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n ).
What is whitespace? Whitespace is any string of text composed only of spaces, tabs or line breaks (to be precise, CRLF sequences, carriage returns or line feeds). These characters allow you to format your code in a way that will make it easily readable by yourself and other people.
When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area on a page. For example, the common whitespace symbol U+0020 SPACE (also ASCII 32) represents a blank space punctuation character in text, used as a word divider in Western scripts.
Use the test() method to check if a string contains whitespace, e.g. /\s/. test(str) . The test method will return true if the string contains at least one whitespace character and false otherwise.
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.
boolean containsWhitespace = false;
for (int i = 0; i < text.length() && !containsWhitespace; i++) {
if (Character.isWhitespace(text.charAt(i)) {
containsWhitespace = true;
}
}
return containsWhitespace;
or, using Guava,
boolean containsWhitespace = CharMatcher.WHITESPACE.matchesAnyOf(text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With