Remove Leading and/or Trailing Whitespaces from Java String

This tutorial provides a few programs to demonstrate how to trim the leading and/or trailing whitespaces from a Java String.

1. Introduction

we have the following string that has whitespace at the start and end. The starting whitespaces are called leading whitespaces, and trailing whitespaces are at the String’s end.

Though, we can use the regex and other methods to trip the spaces, using the strip() methods has been recommended since Java 11.

String blogName = "    how to do in java    ";

//"how to do in java    " - Trimmed leading whitespaces
//"    how to do in java" - Trimmed trailing whitespaces

String strippedLeadingWhitespaces = blogName.stripLeading();
String strippedTrailingWhitespaces = blogName.stripTrailing();
String strippedString = blogName.strip();

2. Remove Only the Leading Whitespaces

2.1. Using String.stripLeading() – Java 11.

The stripLeading() method has been added in Java 11. It returns a string with all leading white spaces removed.

String blogName = "    how to do in java    ";
String strippedLeadingWhitespaces = blogName.stripLeading();

Assertions.assertEquals(strippedLeadingWhitespaces, "how to do in java    ");

2.2. Using Regex

This Java example utilizes regular expression with String’s replaceAll() method to find all leading whitespaces. Once we have all the leading whitespaces, we can replace them with an empty string.

String regex = "^\\s+";
String trimmedLeadingWhitespaces = blogName.replaceAll(regex, "");

Assertions.assertEquals(trimmedLeadingWhitespaces, "howtodoinjava    ");

Or we can use the same regex to match the whitespaces at the beginning to String using replaceFirst() method.

String regex = "^\\s+";
String trimmedLeadingWhitespaces = blogName.replaceFirst(regex, "");

Assertions.assertEquals("howtodoinjava    ", trimmedLeadingWhitespaces);

2. Remove Only the Trailing Whitespaces

2.1. Using String.stripTrailing() – Java 11

The stripTrailing() method returns a string with all trailing white space removed.

String blogName = "    how to do in java    ";
String strippedTrailingWhitespaces = blogName.stripTrailing();

Assertions.assertEquals(strippedTrailingWhitespaces , "how to do in java    ");

2.2. Using Regex

The approach to remove the whitespaces from the end of String is very similar to the previous example. Only the regex changes to match from the end.

String regex = "\\s+$";

String trimmedTrailingWhitespaces = blogName.replaceAll(regex, "");
Assertions.assertEquals("    how to do in java", trimmedTrailingWhitespaces);

3. Remove Leading and Trailing Whitespaces

3.1. Using String.trim() or String.strip()

If we want to remove surrounding whitespaces from string, then the best way is to use String.trim() method.

String blogName = "    how to do in java    ";

String trimmedString = blogName.trim();
Assertions.assertEquals("how to do in java", trimmedString);

We can also use the strip() method which has been available since Java 11.

String blogName = "    how to do in java    ";

String strippedString = blogName.strip();
Assertions.assertEquals("how to do in java", strippedString);

Use the above-given examples to trim leading and trailing white spaces from String in Java.

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