Even if the two strings look the same to you, they may not. For example, they may have leading and trailing spaces. The computer “sees” it, but we don’t. Java has a special trim() method to remove trailing and leading whitespace from a string.
In this article, we are going to explain how to use the Java string trim() method in Java program and provide some code examples.
Java String trim() method signature
The signature of the trim() method in Java is as follows:
public String trim()
As you can see, it takes no parameters and returns a string value. It returns the copy of the original string, but with all leading and trailing spaces removed. Important Note: a space is any character with ascii-code less than or equal to 'U + 0020'.
If our String is an empty sequence of characters, or the first and last characters of the sequence of characters represented by this String object have non-whitespace codes (as defined above), then a reference to this String object is returned.
Well, if all the characters in our string are spaces, then a String object is returned representing an empty string.
If there are no spaces at the beginning and end of the line, the method will return the line itself unchanged.
Java String trim() Method Example
Let’s have two strings with and without some spaces at the beginning and end of their text. The text will be the same for both of the Strings.
public class TrimDemo {
public static void main(String[] args) {
//java string trim() method example
String noSpacesString = "I am the Walrus";
String withSpaceString = " I am the Walrus ";
//printing the both strings out to make sure they are different
System.out.println(noSpacesString);
System.out.println(withSpaceString);
//let's print out two string comparisons. It's false
//because they are different
System.out.println(noSpacesString.equals(withSpaceString));
//trim
withSpaceString.trim();
System.out.println(noSpacesString.equals(withSpaceString));
System.out.println(noSpacesString);
System.out.println(withSpaceString);
}
}
The output is:
I am the Walrus
I am the Walrus
false
false
I am the Walrus
I am the Walrus
Wait, what?? It seems that the Java string trim() method doesn’t work! The first two strings are different, as they should be (the first has no margins, the second has three spaces in front and one after the text). Then we compared the two lines and printed the result out. It is obviously false as these lines are different. After that, we applied the trim method to the string with spaces along the edges and compared the strings again ... and for some reason we got false again, although now they should be the same!
Then, just in case, we decided to print the lines themselves, what if a miracle happens? Alas... The lines were different and still remain. Have you already guessed what the problem is? The point is that strings in Java are immutable objects. So the trim() method returns not the original string, but a new one.
Anyway, we should test the trim() method here, so let's write another program. Here we are going to name a copy of the string created as a result of the trim() method to work with. Let's also check the two strings for equivalence.
public class TrimDemo2 {
public static void main(String[] args) {
String noSpacesString = "I am the Walrus";
String withSpaceString = " I am the Walrus ";
String result = withSpaceString.trim();
System.out.println(noSpacesString);
System.out.println(result);
System.out.println(noSpacesString.equals(result));
}
}
Here is the output:
I am the Walrus
I am the Walrus
true
Bingo! Now it works as we expected. Remember about String immutability, it’s very important for every Java developer.Conclusion
So that’s your guided tour of the String.trim()
method in Java! You’ve seen how it removes those pesky leading and trailing whitespaces, gotten a glimpse of why that’s so handy for sanitizing user input, and even learned about potential pitfalls, like dealing with special Unicode spaces. Overall, trimming strings can be a simple yet powerful tool for keeping your data clean and consistent. Now that you know all about trim()
, feel free to experiment—try mixing it with other string manipulation methods, see what happens with different whitespace characters, and figure out how it can best serve your application’s needs. Keep up the great work, and happy coding!
GO TO FULL VERSION