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

In the last tutorial, we have learned about string class methods. In this tutorial, we will learn how to remove leading and trailing whitespaces from String. 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.



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. Let's understand with an example.

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 run fine there is no problem at all. 
Java string trim() method

But there are two biggest problems in this code from the programmer 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 woks. Let's see the example.
Java string trim() method

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 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 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. 
Java string trim() method

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. 


Java String trim() Method Example


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
How to remove leading and trailing whitespaces from String in Java? How to remove leading and trailing whitespaces from String in Java? Reviewed by Prashant Srivastava on January 09, 2020 Rating: 5

No comments:

Powered by Blogger.