How do I check if a character is a whitespace in Java?

Whitespace characters in Java (or programming in general) aren’t just the space ' ' character. It also includes other characters that create some form of space or break in the text. The most common ones include:

  • space ' '
  • tab '\t'
  • newline '\n'
  • carriage return '\r'
  • form feed '\f'.

All these characters fall into the category of whitespace characters.

Now, if we want to check if a character in Java is one of these whitespace characters, we can make use of the built-in method Character.isWhitespace(char ch). Character is a class in Java that provides a number of useful class (i.e., static) methods for working with characters. And the isWhitespace() method is one of them which checks if the provided character is a whitespace character.

Here is a simple code snippet:

package org.kodejava.lang;

public class CharacterIsWhitespace {
    public static void main(String[] args) {
        char ch = ' ';

        if (Character.isWhitespace(ch)) {
            System.out.println(ch + " is a whitespace character.");
        } else {
            System.out.println(ch + " is not a whitespace character.");
        }
    }
}

This code first defines a character ch and then uses Character.isWhitespace(ch) to check if it is a whitespace character. The isWhitespace() method returns true if the given character is a space, new line, tab, or other whitespace characters, false otherwise.

Here’s a little more expansive example:

package org.kodejava.lang;

import java.util.Arrays;
import java.util.List;

public class CharacterIsWhitespaceDemo {
    public static void main(String[] args) {
        List<Character> characters = Arrays.asList(' ', '\t', '\n', '\r', '\f', 'a', '1');
        for (char ch : characters) {
            if (Character.isWhitespace(ch)) {
                System.out.println("'" + ch + "' is a whitespace character.");
            } else {
                System.out.println("'" + ch + "' is not a whitespace character.");
            }
        }
    }
}

Output:

' ' is a whitespace character.
'   ' is a whitespace character.
'
' is a whitespace character.
' is a whitespace character.
'' is a whitespace character.
'a' is not a whitespace character.
'1' is not a whitespace character.

In this code snippet, we are checking and outputting whether each character in a list of characters is a whitespace character or not. The list includes a space, a tab, newline, carriage return, form feed, an alphabetic character, and a digit. The isWhitespace() method identifies correctly which ones are the whitespace characters.

The Character.isWhitespace(char ch) method in Java also considers Unicode whitespace. It checks for whitespace according to the Unicode standard. The method considers a character as a whitespace if and only if it is a Unicode space separator (category “Zs”), or if it is one of the following explicit characters:

  • U+0009, HORIZONTAL TABULATION (‘\t’)
  • U+000A, LINE FEED (‘\n’)
  • U+000B, VERTICAL TABULATION
  • U+000C, FORM FEED (‘\f’)
  • U+000D, CARRIAGE RETURN (‘\r’)

Here is an example of checking Unicode whitespace:

package org.kodejava.lang;

public class CharacterIsWhitespaceUnicode {
    public static void main(String[] args) {
        char ch = '\u2003';  // EM SPACE

        if (Character.isWhitespace(ch)) {
            System.out.println("Character '" + ch + "' (\\u2003) is a whitespace character.");
        } else {
            System.out.println("Character '" + ch + "' (\\u2003) is not a whitespace character.");
        }
    }
}

Output:

Character ' ' (\u2003) is a whitespace character.

In this example, \u2003 is a Unicode representation of the “EM SPACE” character, which is a type of space character in the Unicode standard. The isWhitespace() method correctly identifies it as a whitespace character.

How do I split a string with multiple spaces?

This code snippet show you how to split string with multiple white-space characters. To split the string this way we use the "\s+" regular expression. The white-space characters include space, tab, line-feed, carriage-return, new line, form-feed.

Let’s see the code snippet below:

package org.kodejava.lang;

import java.util.Arrays;

public class SplitStringMultiSpaces {
    public static void main(String[] args) {
        String text = "04/11/2021    SHOES      RUNNING RED   99.9 USD";

        // Split the string using the \s+ regex to split multi spaces
        // line of text.
        String[] items = text.split("\\s+");
        System.out.println("Length = " + items.length);
        System.out.println("Items  = " + Arrays.toString(items));
    }
}

The result of the code snippet is:

Length = 6
Items  = [04/11/2021, SHOES, RUNNING, RED, 99.9, USD]

How do I split up string using regular expression?

This code snippet uses the java.util.regex.Pattern.split() method to split-up input string separated by commas or whitespaces (spaces, tabs, new lines, carriage returns, form feeds).

package org.kodejava.regex;

import java.util.regex.Pattern;

public class RegexSplitExample {
    public static void main(String[] args) {
        // Pattern for finding commas, whitespaces (spaces, tabs, new lines,
        // carriage returns, form feeds).
        String pattern = "[,\\s]+";
        String colors = """
                Red,White, Blue   Green        Yellow,
                Orange Pink""";

        Pattern splitter = Pattern.compile(pattern);
        String[] results = splitter.split(colors);

        for (String color : results) {
            System.out.format("Color = \"%s\"%n", color);
        }
    }
}

The result of our code snippet is:

Color = "Red"
Color = "White"
Color = "Blue"
Color = "Green"
Color = "Yellow"
Color = "Orange"
Color = "Pink"

How do I remove trailing white space from a string?

The trim() method of a String class removes both leading and trailing white space from a string. In this example we use a regular expression to remove only the trailing white spaces from a string.

package org.kodejava.lang;

public class TrailingSpace {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // Using a regular expression to remove only the trailing white space in
        // a string
        text = text.replaceAll("\\s+$", "");
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}
Original      =      tattarrattat     
text.length() = 22
Result        =      tattarrattat
text.length() = 17

How do I remove leading white space from a string?

The trim() method of a String class removes both leading and trailing white space from a string. In this example we use a regular expression to remove only the leading white spaces from a string.

package org.kodejava.lang;

public class LeadingSpace {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // Using regular expression to remove only the leading white
        // space in string
        text = text.replaceAll("^\\s+", "");
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}
Original      =      tattarrattat     
text.length() = 22
Result        = tattarrattat     
text.length() = 17