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:
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:
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:
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:
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:
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.