Sign in
Log inSign up
Different ways to Remove Spaces from String In Java

Different ways to Remove Spaces from String In Java

Ankit Dixit's photo
Ankit Dixit
·Nov 30, 2021·

4 min read

Understanding the problem:

Given a string, We need to remove spaces from it. The spaces may be leading spaces, trailing spaces, or empty spaces in between the words.

Examples: -

Input: “ Hello World! ”

Output 1: “Hello World!” // removing all trailing, leading and in between extra spaces.

Output 2: “ Hello World!” // removing only trailing spaces.

Output 3: “Hello World! ” // removing only leading spaces.

Output 4: “ Hello World! ” // removing only in between extra spaces

The task can be accomplished in many ways, Some ways may involve something manual like using a for loop and doing it, while the other includes the use of some inbuilt java libraries for it.

Given below are some of the ways of accomplishing the task: -

1. Using Iteration: - Iterating the string and keeping a list of characters, whenever we get a whitespace we look into the list and append it only if the last character is not a whitespace. At last we remove the trailing one extra space if any.

Output: - Input string is :" Cool is Cool " Output string is :"Cool is Cool"

2. Using trim() method: -

trim() is a built-in function in Java String that removes leading and trailing spaces. 'u0020' is the Unicode value for the space character. If this Unicode value exists before and after the text, the trim() method in Java removes the spaces and returns the omitted string.

Note: It doesn’t remove the extra whitespace in between the string.

Examples using Java Code:

Output

Input string is :" Cool is Cool " Output string is :"Cool is Cool"

3. Strip() method : -

From Java 11 onwards, the Java String class has a strip() method for removing leading and trailing white spaces. Internally, this method checks for whitespace using Character.isWhitespace().

Java Code with example: -

Output : -

Input string is :" Cool is Cool " Output string is :"Cool is Cool"

There are two other ways to get rid of either leading or trailing spaces separately.

i. Using stripLeading(): - This function only removes the extra spaces leading the sentence.

Java Example: -

Output: -

Input string is :" Cool is Cool " Output string is :"Cool is Cool "

ii. Using stripTrailing(): - This function only removes the extra spaces trailing the sentence.

Java Example: -

Output: -

Input string is :" Cool is Cool " Output string is :" Cool is Cool"

4. Using replaceAll(String regex, String replacement) method : -

This is one of the most powerful methods for string manipulation, and it was added in Java 1.4. We can replace each matching regular expression substring with the specified replacement string using the replaceAll() method. For instance, to remove all spaces, all leading spaces, all trailing spaces, and so on.

All we need to do now is generate the proper regular expression with the proper replacement parameter.

Regex Used Target Achieved \s+ Find all whitespaces in a string ^\s+ Find all leading whitespaces in the string \s+$ Find all trailing whitespaces in the string

Java Code Example: -

Note:- In the code below instead of using “\s+” as regex I am using “\s+” as regex to ignore ‘\’ of the regex. In java, we use ‘\’ in front of any character to ignore the function of it.

You can run your code online here

Output:-

Input string is :" Cool is Cool " Output string using regex as \s+ is :"CoolisCool" Output string using regex as ^\s+ is :"Cool is Cool " Output string using regex as \s+$ is :" Cool is Cool"

Conclusion: -

That's all for the topic “Different ways to Remove Spaces from String In Java”. The topic covered almost every possible way of removing spaces from the string in Java. It started from the method of iteration and then also touched every built-in function to remove trailing, leading and extra spaces inside the string. It briefly covers the use of trim(), strip(), stripLeading(), stripTrailing() and replaceAll() functions with working examples of each. Further, Corrections and more suggestions are forever welcome.