The trim() method of Java removes the whitespaces from the left and right of the given string.
The leading or trailing spaces may occur particularly, as you are taking user input. Similarly, for database driven strings, you may ensure the strings do not include the leading or trailing spaces by using the trim() method.
Syntax of trim Java method
Following is the general syntax for using the trim method:
- Where Str is the Java string that you want to remove space from.
- If the specified string contains leading and/or trailing spaces, the trim() returns a copy of the string with whitespaces omitted.
- If the specified string does not contain a leading or trailing whitespace, it returns this string.
- Basically, the trim() function checks the ‘\u0020’ Unicode character which represents a space character.
An example of Java string trim method
In the following example, a string is created with leading and trailing spaces.
After that, the trim method is used and the string is displayed.
To make things clear, the string before and after using the trim method is displayed so you may see the difference:
public class trimTest { public static void main(String []args) { //A demo of using trim() method String str_trim = " A string for trim demonstration "; System.out.println(str_trim); System.out.println(str_trim.trim()); } }
The output:
You can see, the leading and trailing spaces are removed from the specified string.
Using the \t (tab) with trim method example
Yes, the trim() function will also remove spaces created due to \t i.e. tab.
See the following example where a string is created with \t towards both sides. Again, for clarification, the original string is displayed along with the copy returned after using the trim():
public class trimTest { public static void main(String []args) { //A demo of using trim() method String str_trim_tab = "\tString containing tabs to be trimmed\t"; System.out.println(str_trim_tab); System.out.println(str_trim_tab.trim()); } }
Output:
What about trimming only left or right spaces?
Well, as we have seen in the definition and examples, the trim method can be used for removing the leading and trailing spaces.
However, in certain scenarios, you may require omitting whitespaces from one side – left or right of the string.
There is no specific method for that like ltrim to remove leading spaces or rtrim for removing the trailing space.
You may use different ways and one of the ways is using the replaceAll method as shown in the example below:
public class trimTest { public static void main(String []args) { //A demo of using trim() method String str_trim_l_r = " Removing leading or trailing spaces "; System.out.println("Original String: " + str_trim_l_r +" Next String \n" ); System.out.println("Trimmed String: " + str_trim_l_r.trim() +" Next String \n"); System.out.println("Left Trimmed: " + str_trim_l_r.replaceAll("^\\s+","") +" Next String \n"); System.out.println("Right Trimmed: " + str_trim_l_r.replaceAll("\\s+$","") +" Next String"); } }
You can see four strings as output:
- The first is the original string with a concatenated string to display spaces towards the right side.
- The second string used the trim() function, so removed spaces from both sides.
- In third string, the leading spaces (from the left side) are removed. You can see three spaces between the original and concatenated string towards the right side.
- Similarly, the last statement displays the string with spaces removed from the right side only.