Java

How to Use String trim() Method in Java?

The trim() method is a built-in method in the String class in Java, and it is utilized to remove whitespace (spaces, tabs, and newlines) from both the beginning and the end of a string. The trim() method returns the string object that is like the actual string with all leading whitespace removed. It is useful for cleaning up user input, comparing strings, and cleaning up data.

This article will illustrate the String trim() Method in Java with practical implementation.

How to Use the String trim() Method in Java?

The trim() method in Java can be utilized for removing the trailing white spaces from the provided string. Here is the syntax of the trim() method:

public String trim()

In the syntax, the trim() method does not modify the original string object. Instead, it returns an updated string object that includes the trimmed version of the original string.

Example 1: Remove the Leading and Trailing Whitespace

Here is an example program that demonstrates the usage of the trim() method in Java:

public class Main {
    public static void main(String[] args) {
        String str = "   Hello, World!   ";
        System.out.println("Original String: " + str);

        String trimmedStr = str.trim();
        System.out.println("Trimmed String: " + trimmedStr);
    }
}

The explanation of the above code is given below:

  • Create a String object str that contains some leading and trailing whitespace.
  • Then, call the trim() method on this string and assign the result to a new String object called trimmedStr.
  • Finally, print both the original string and the trimmed string to the console.

Output

The output of the program the trim() method has removed the leading and trailing whitespace from the original string and returned a new string object that contains the trimmed version of the string.

Example 2: Removing White Spaces from User Input

When a user inputs data, it can sometimes contain unwanted leading or trailing spaces. To remove these spaces, use the trim() method. Here is an example:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = scanner.nextLine().trim();
        System.out.println("Hello, " + name + "!");
    }
}

The description of the code is below:

  • Use the Scanner class to read user input.
  • Then, call the trim() method on the input string for removing the trailing or leading spaces before printing a greeting message to the console.

Example 3: Comparing Strings

When comparing two strings, you may want to ignore leading or trailing white spaces. The trim() method can be useful in such situations. Here is an example:

public class Main {
    public static void main(String[] args) {
        String str1 = "   Hello, World!   ";
        String str2 = "Hello, World!";
        boolean isEqual = str1.trim().equals(str2);
        System.out.println(isEqual);
    }
}

In this code,

  • Create two strings “str1” and “str2”. str1 contains some leading and trailing spaces.
  • Use the trim() method on str1 to remove the spaces and then compare the resulting string to str2 using the equals() method.

Output

The output of the program is true because the trimmed version of str1 is equal to str2.

Example 4: Cleaning up Data

When working with data, users may need to clean it up by removing unwanted spaces. The trim() method can be useful in such situations. Here is an example:

public class Main {
    public static void main(String[] args) {
        String data = "   12345   ";
        int number = Integer.parseInt(data.trim());
        System.out.println(number);
    }
}

In this example,

  • A string data that contains a number with some leading and trailing spaces.
  • Use the “trim()” method to remove the spaces and then convert the resulting string to an integer using the “parseInt()” method.

Output

The output of the program is 12345, which is the trimmed version of the original string converted to an integer.

Conclusion

The “trim()” method in Java is utilized for removing whitespace from the starting and end of the given string. It keeps the trimmed version of the original string by returning a new string object. The trim() method is useful for removing unwanted whitespace from user input or for comparing strings without regard to leading or trailing whitespace. This guide has illustrated the trim() method in Java.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.