In Java, removing whitespace from a given String is a common need when manipulating strings. This article will discuss eliminating all or a portion of the existing whitespace in the string. We'll look at numerous built-in Java methods to accomplish the task of remove whitespaces from a string: The article aims to: Suppose you have to display a list of strings, but each of these strings has some spaces at the end, added mistakenly. This needs to be removed before displaying. Likewise, there can be spaces in between or at the
beginning of the strings as well. We shall be looking at Java programs to remove these whitespaces. Also, Java provides numerous in-built methods to perform this task. To list a few:Overview
Scope
Introduction
Let us discuss each of these methods to remove unnecessary spaces from a String one by one.
trim() method in Java
This is the most common method to remove white spaces present at the beginning and end of the string. For trim() method, space character is a character having ASCII values <= 32. The Unicode value of space character is '\u0020'. It removes all characters between '\u0000' (NUL) and '\u0020' (SPACE) from the start and end of the string. It checks for these Unicode values at the beginning and end of the string. If present, it removes the spaces and returns the modified string.
Signature
trim() method is defined as:
Syntax
trim() method is written as
Parameters
trim() method takes no parameters.
Return Values
It returns the modified version of the original string, with no spaces at the end and beginning.
Exceptions
It throws
- NullPointerException: If the string on which method is called is null.
Usage of trim() method
Program to Remove Leading and Trailing Whitespaces
class Main { public static void main(String args[]) { // trims the trailing and leading spaces //string with single trailing and leading spaces String s = " Hey! Let us learn Java! "; System.out.println(s.trim()); //string with multiple trailing and leading spaces String s1 = " Learning Java is fun! "; System.out.println(s1.trim()); } }
Output:
Hey! Let us learn Java! Learning Java is fun!
Explanation:
The trim() method is used in the program above to trim the strings s and s1. In contrast to s1, which contains numerous spaces, s only has one space at the beginning and one at the end. These spaces are eliminated once the method trim() is called and the output is printed.
Program where exception is thrown:
Let us look at an example where we call trim() method on a null string.
Code:
class Main { public static void main(String args[]) { // trims the trailing and leading spaces String s = null; System.out.println(s.trim()); } }
Output:
Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:10)
Explanation:
In the above program, the value of the string is null; hence exception is thrown.
strip() method Java 11
The strip() method was introduced in Java 11. It also removes leading and trailing spaces present in the string. But that is exactly what trim() does. What's the difference between the two?
strip() method is Unicode aware, a modification over trim().The reason behind the addition of this method is that there exist numerous space characters as per the Unicode standards having ASCII value > 32(‘U+0020’); the strip() method can remove them as well.
It was introduced in Java 1. | It was introduced in Java 11. |
It makes use of ASCII values. | It makes use of the Unicode value. |
It removes space characters with ASCII values less than or equal to 32(‘\u0020’) | It removes all the space characters as per the Unicode standards having ASCII value greater than 32(‘\u0020’) as well |
Signature
strip() method is defined as:
Syntax
strip() method is written as
Parameters
strip() method takes no parameters.
Return Values
It returns the modified version of the original string, i.e., with no spaces at the end and beginning.
Exceptions
It throws
- NullPointerException: If the string on which method is called is null.
Usage of strip() method
Program to Remove Leading and Trailing Whitespaces
public class Main { public static void main(String[] args) { // string with whitespace characters as per unicode values String str1 = '\u2003' + "Learn Java "; System.out.println("Original String:" + "==" + str1 + "=="); System.out.println("with trim method: " + "==" + str1.trim() + "=="); System.out.println("with strip method: " + "==" + str1.strip() + "=="); } }
Output:
Original String: == Learn Java == with trim method: == Learn Java== with strip method: ==Learn Java==
Explanation:
In the above program, we have appended a whitespace character with Unicode value: ‘\u2003’. So as you can see, the trim() method does not remove it as the value is not less than '\u0020'. Whereas in the case of strip(), it gets removed. This pretty much explains the advantage of strip() over trim().
stripLeading() method Java 11
stripLeading() method removes all the leading spaces or the spaces present at the beginning of the string. It was introduced in Java 11. Just like strip(), it can remove the space characters as per the Unicode standards having ASCII value greater than 32(‘\u0020’) as well.
Signature
stripLeading() method is defined as:
public String stripLeading()
Syntax
stripLeading() method is written as
Parameters
stripLeading() method takes no parameters.
Return Values
It returns the modified version of the original string, i.e., with no spaces at the beginning. Exceptions: It throws
- NullPointerException: If the string on which method is called is null.
Usage of stripLeading() method
Program to Remove Leading Whitespaces
public class Main { public static void main(String[] args) { //Original String String str1 = " Learn Java "; System.out.println("String 1:" + "==" + str1 + "=="); // string with whitespace character as per Unicode values String str2 = '\u2003' + "Learn Java "; // trims the leading spaces System.out.println("String 2:" + "==" + str2 + "=="); System.out.println("modified string 1: " + "==" + str1.stripLeading() + "=="); System.out.println("modified string 2: " + "==" + str2.stripLeading() + "=="); } }
Output:
String 1: == Learn Java == String 2: == Learn Java == modified string 1: ==Learn Java == modified string 2: ==Learn Java ==
Explanation:
In the above program, the stripLeading() method is called on two strings. One of these has a space character with a codepoint value > 32(‘\u0020’). As you can see, in both cases, the spaces present at the beginning get removed; however, the ones at the end are still present.
stripTrailing() method Java 11
This method is exactly the opposite of the stripLeading() method. It removes all the trailing or the spaces present at the end of the string. It was also introduced in Java11. See the image below for reference:
Signature
stripTrailing() method is defined as:
public String stripTrailing()
Syntax
stripTrailing() method is written as
Parameters
stripTrailing method takes no parameters.
Return Values
It returns the modified string with no spaces at the end.
Exceptions
It throws
- NullPointerException: If the string on which method is called is null.
Usage of stripTrailing() method
Program to Remove Trailing Whitespaces
public class Main { public static void main(String[] args) { //Original String String str1 = " Learn Java "; System.out.println("String 1:" + "==" + str1 + "=="); // string with whitespace character as per Unicode values String str2 = " Learn Java" + '\u2003'; System.out.println("String 2:" + "==" + str2 + "=="); // trims the trailing spaces System.out.println("modified string 1: " + "==" + str1.stripTrailing() + "=="); System.out.println("modified string 2: " + "==" + str2.stripTrailing() + "=="); } }
Output:
String 1: == Learn Java == String 2: == Learn Java == modified string 1: == Learn Java== modified string 2: == Learn Java==
Explanation:
In the above program stripLeading() method is called on two strings. One of these has a space character with a codepoint value > 32(‘\u0020’). As you can see, in both cases, the spaces present at the end get removed. However, the ones at the beginning are still present.
replaceAll(String regex, String replacement)
replaceAll() is a very commonly used method for string manipulation in Java. This method replaces all the substrings of the string matching the specified regex expression with the specified replacement text.
A regular expression or regex is a string pattern that we use for some searching or matching kind of operations.
We can use this method to remove spaces from the string. All we need to do is specify the regex expression for the kind of spaces that need to be removed, i.e., trailing or leading, or both. The following table shows the regex expressions for the same:
\s+ | For all the spaces |
^\s+ | For the leading spaces |
\s+$ | For the trailing spaces |
Signature
replaceAll() method is defined as:
public String replaceAll(String regex, String replacement)
Syntax
replaceAll() method is written as
string.replaceAll(regex, replacement)
Parameters
replaceAll method takes two parameters:
- String regex: The regular expression that is used for matching the substring.
- String replacement: The string that replaces the matched substring.
Return Values
It returns the modified string, where all the occurrences of matched substrings are replaced by the string specified as a parameter.
Exceptions
It throws :
- PatternSyntaxException : If the syntax of the regex is invalid.
Usage of replaceAll() method
Program to Remove All Whitespaces and Leading, Trailing Whitespaces
public class Main { public static void main(String[] args) { String str1 = " Learn Java "; //original string System.out.println("String 1:" + "==" + str1 + "=="); System.out.println( "Replace all the spaces " + "==" + str1.replaceAll(" ", "") + "==" ); //regex to remove all the spaces System.out.println( "Replace all using regex " + "==" + str1.replaceAll("\\s+", "") + "==" ); //regex to remove all the leading spaces System.out.println( "Replace all the leading spaces: " + "==" + str1.replaceAll("^\\s+", "") + "==" ); //regex to remove all the trailing spaces System.out.println( "Replace all the trailing spaces: " + "==" + str1.replaceAll("\\s+$", "") + "==" ); } }
Output
String 1:== Learn Java == Replace all the spaces ==LearnJava== Replace all using regex ==LearnJava== Replace all the leading spaces: ==Learn Java == Replace all the trailing spaces: == Learn Java==
Explanation
In the above program, replaceAll() is called with different regular expressions as parameters to remove leading, trailing, and all the spaces.
Program where Exception is thrown
Code:
public class Main { public static void main(String[] args) { String str1 = " Learn Java "; //original string System.out.println("String 1:" + "==" + str1 + "=="); System.out.println( "Replace all using regex " + "==" + str1.replaceAll("[s", "") + "==" ); } }
Output:
String 1:== Learn Java == Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 1 [s ^ at java.base/java.util.regex.Pattern.error(Pattern.java:2027) at java.base/java.util.regex.Pattern.clazz(Pattern.java:2695) at java.base/java.util.regex.Pattern.sequence(Pattern.java:2138) at java.base/java.util.regex.Pattern.expr(Pattern.java:2068) at java.base/java.util.regex.Pattern.compile(Pattern.java:1782) at java.base/java.util.regex.Pattern.<init>(Pattern.java:1428) at java.base/java.util.regex.Pattern.compile(Pattern.java:1068) at java.base/java.lang.String.replaceAll(String.java:2126) at Main.main(Main.java:9)
Explanation:
So in the above program, the syntax defined for regex is incorrect; hence exception is thrown.
Note: We can not directly use "" in Java, escape character is required. Thus, "\" is used instead.
Conclusion
- Java has inbuilt methods to remove the whitespaces from the string, whether leading, trailing, both, or all.
- trim() method removes the leading and trailing spaces present in the string.
- strip() method removes the leading and trailing spaces present in the string. Also, it is Uniset/Unicode character aware.
- stripleading() method removes the spaces present at the beginning of the string.
- striptrailing() method removes the spaces present at the end of the string.
- replaceall(String regex, String replacement) method replaces all the matched substring with a new string.