The trim function in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of the space character is ‘\u0020’. The trim() method in Java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string. The trim() in Java also helps in trimming the characters.
Note: The trim() method doesn’t eliminate middle spaces.
Method Signature
public String trim()
Parameters
- The trim() method accepts no parameters.
Return Type
- The return type of trim() method is String. It returns the omitted string with no leading and trailing spaces.
Below are examples to show the working of the string trim() method in Java.
Examples of Trim() in Java
Example 1:
The trim function to remove whitespaces from the left and right of the string can be used when we want to join multiple strings together.
Below is the implementation of the topic:
Java
// Java program to demonstrate working // of java string trim() method import java.io.*; // Driver Class class GFG { // Main Function public static void main (String[] args) { // Three strings declared String x= "geeks " ; String y= "for " ; String z= "geeks" ; // Printing without trim function System.out.println(x+y+z); // Using trim function to get result System.out.println(x.trim()+y.trim()+z.trim()); } } |
geeks for geeks neveropen
Example 2:
After using the trim function it returns the string rather than making changes to the original string.
Below is the implementation of the above topic:
Java
// Java program to demonstrate working // of java string trim() method class Gfg { // driver code public static void main(String args[]) { // trims the trailing and leading spaces String s = " geeks for geeks has all java functions to read " ; // Printing String after removing the whitespaces // from the string System.out.println(s.trim()); // Printing string to observe System.out.println(s); } } |
geeks for geeks has all java functions to read geeks for geeks has all java functions to read
Time Complexity: O(n)
Auxiliary Space: O(1)
Example 3:
While using the trim function as we get two strings original and returned string both are different in case we are removing the whitespaces from the original string.
Below is the implementation of the above topic:
Java
// Java program to demonstrate working // of java string trim() method import java.io.*; // Driver Class class GFG { // Main Function public static void main(String[] args) { // String declared String s1 = " Geeks For Geeks " ; // Before Trim() method System.out.println( "Before Trim() - " ); System.out.println( "String - " + s1); System.out.println( "Length - " + s1.length()); // applying trim() method on string s1 String s2 = s1.trim(); // After Trim() method System.out.println( "\nAfter Trim() - " ); System.out.println( "String - " + s2); System.out.println( "Length - " + s2.length()); // Comparing both the strings if (s1 == s2) { System.out.println( "\nEqual" ); } else { System.out.println( "\nNot Equal" ); } } } |
Before Trim() - String - Geeks For Geeks Length - 21 After Trim() - String - Geeks For Geeks Length - 15 Not Equal
Time Complexity: O(n)
Auxiliary Space: O(1)