Java – Normalize Extra White Spaces in a String

Learn to remove extra white spaces between words from a String in Java. Given 3 examples replace the multiple spaces with a single space using a regular expression, StringBuiffer and Apache Commons StringUtils class.

1. Using Apache Common’s StringUtils

This approach, using StringUtils.normalizeSpace() is most readable and it should be the preferred way to remove unwanted white spaces between words.

This function returns the argument string with whitespace normalized in two steps i.e. –

1.1. Maven Dependency

Add the latest version of commons-lang3 from Maven repo.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

1.2. Java Program to Remove Extra Spaces

// uneven spaces between words
String blogName = "how to   do    in  java   .         com"; 
 
String nameWithProperSpacing = StringUtils.normalizeSpace( blogName );
 
System.out.println( nameWithProperSpacing );

Program output.

how to do in java . com

See Also: Removing All White Spaces from a String in Java

2. Using Regular Expression

Using a regular expression, to replace 2 or more white spaces and non-visible characters with a single space is also a good solution.

We are using regex pattern as “\\s+”.

  1. \s matches a space, tab, new line, carriage return, form feed or vertical tab.
  2. + says one or more occurrences.

Note that this method will NOT trim the String. That means there might be a single space at the start at end of string, if the original string has such white spaces at the beginning or end.

// uneven spaces between words
String blogName = "how to   do    in  java   .         com"; 
 
String nameWithProperSpacing = blogName.replaceAll("\\s+", " ");
 
System.out.println( nameWithProperSpacing );

Program output.

how to do in java . com

See Also: Removing leading spaces and Removing trailing spaces from a String

3. Using StringBuffer

StringBuiffer can also help us to get rid of unevenly spaced strings. This is a complete programmatic approach and may not be suitable for large strings.

Please note that similar to the regular expression example, this method also may leave leading and trailing white spaces. So do not forget to trim the string after you have removed extra whitespaces between words in the parameter string.

// uneven spaces between words
String blogName = "how to   do    in  java   .         com"; 
 
StringTokenizer st = new StringTokenizer(blogName, " ");
StringBuffer sb = new StringBuffer();
 
while(st.hasMoreElements())
{
    sb.append(st.nextElement()).append(" ");
}
 
String nameWithProperSpacing = sb.toString();
 
System.out.println( nameWithProperSpacing );
 
//trim leading and trailing white spaces
nameWithProperSpacing = nameWithProperSpacing.trim();
     
System.out.println( nameWithProperSpacing );

Program output.

how to do in java . com

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial