How to remove Leading and Trailing Whitespaces from String in Java? | Trim Spaces from Start and End of a String

In this tutorial, we will learn how to remove leading and trailing whitespaces from String. There are various methods to remove the leading and trailing spaces in Java. Java String trim() method is used to remove leading and trailing whitespaces from String. In this tutorial, we will learn the trim() method with a small case study. So that, you can understand Java String trim() method with much clarity. Here, we will discuss first what is the need for that method. We will explain its need with an example.

String.replaceAll() Method

replaceAll() Method finds all the leading whitespace. Once you find all the leading spaces you can replace them with an empty string.

Remove Leading Spaces using replaceAll() Method

String.replaceFirst() Method

As an alternative method, you can also go with the replaceFirst() also. It considers a regular expression and searches for the first occurrence at the beginning of the string. Then the method matches the replaced string part with the string value passed as the second argument.

replaceFirst() Method to trim leading and trailing white spaces

Java String trim() Method

Java String trim() method is used to remove whitespaces either from the beginning and the end of a string. It returns a copy of the string, with leading and trailing whitespace omitted.

Also, Refer:

How does Java trim() Method Work?

The Unicode Value for Space Character is ‘\u0020’. This method checks for the Unicode value before and after the string and if it is present then eliminates the space and returns without any spaces.

Trim Leading and Trailing Spaces of a String in Java

Output:

  Hello World  
Hello World
      Hey  there    Joey!!!  
Hey  there    Joey!!!

Example Program to Remove Leading and Trailing Spaces in Java

import java.util.Scanner;
class TrimExample{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Country Name: ");
String country = sc.nextLine();

if(country.equals("india")){
System.out.println("Capital of india is delhi");
}
else if(country.equals("australia")){
System.out.println("Capital of australia is canberra");
}
else if(country.equals("england")){
System.out.println("Capital of england is london");
}
else{
System.out.println("Please enter a valid country name.");
}
}
}

Here, I wrote a simple program where the user entered a country name, and corresponding to the entered country name user will see a message where the country capital is printed. Let’s run this program. The code compiled and runs fine there is no problem at all.

How to remove leading and trailing whitespaces from String in Java 1

But there are two biggest problems in this code from the programmer’s point of view.

1. In this program, the end-user is responsible to enter the country name. The end-user entered the country name either lower case or upper case or mixture. But our program always considered lower case only. If the end-user entered country name only in lower case then only our program works. By mistake, if the end-user entered the country name in the upper case letter then our program will not wok. Let’s see the example.

How to remove leading and trailing whitespaces from String in Java 2

There are two solutions to the above problem. First, We can replace equals() method with equalIgnoreCase(). But if in your program there are 1000 lines or 10000 lines then you need to replace the equals() method with an equalIgnoreCase() method with 1000 or 10000 times, which is not a good practice.

The second solution is, as we know our program accepts only lower case letters. So, what we will do is, If the end-user entered the country name we will convert it into a lower case letter. Then our problem will be solved. So the second solution is very easy because we need to write only one line. See the example below.

import java.util.Scanner;
class TrimExample{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Country Name: ");
String country = sc.nextLine().toLowerCase();

if(country.equals("india")){
System.out.println("Capital of india is delhi");
}
else if(country.equals("australia")){
System.out.println("Capital of australia is canberra");
}
else if(country.equals("england")){
System.out.println("Capital of england is london");
}
else{
System.out.println("Please enter a valid country name.");
}
}
}

Output:

Enter Country Name: INdia
Capital of india is delhi
Enter Country Name: ENGLand
Capital of england is london
Enter Country Name: AUSTRALIA
Capital of australia is canberra

2. But still, there is one more problem with this code. As we know, the end-user is responsible to enter the country name. If the end-user entered the country name with some spaces at the starting or ending then our program check is there some spaces followed by a country name is available or not. In our code, this case is not available, then our code will not works and show a message to the end-user please enter a valid country name. Let’s see the example.

How to remove leading and trailing whitespaces from String in Java 3

To resolve this problem, What we can do, before comparing the country name if any spaces entered by the end-user either at the beginning or at the end, If we remove those spaces then our problem will be solved. We can remove spaces either at the beginning or end of the string by using the trim() method.

Removing Leading and Trailing WhiteSpaces from a String in Java Example Program

import java.util.Scanner;
class TrimExample{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Country Name: ");
String country = sc.nextLine().toLowerCase().trim();

if(country.equals("india")){
System.out.println("Capital of india is delhi");
}
else if(country.equals("australia")){
System.out.println("Capital of australia is canberra");
}
else if(country.equals("england")){
System.out.println("Capital of england is london");
}
else{
System.out.println("Please enter a valid country name.");
}
}
}

Output:

Enter Country Name: INdia
Capital of india is delhi
Enter Country Name: ENGLand
Capital of england is london
Enter Country Name: AUSTRALIA
Capital of australia is canberra