Trim() in Java String

Trim() in Java String | Here, we will see the trim() method defined in the Java string class. This is a string method that omits the whitespaces in the string. The trim() method is present in the string class and returns the string after the removal of the whitespace. Observe the below examples to understand more:-

Example:-
String:- " Java Coding Language "
Resultant string after calling string.trim():- “Java Coding Language”

The method details of the trim() method are as follows,
Method signature:- public string trim()

The trim() method doesn’t take any parameter and returns a string whose value is this string, with all leading and trailing space removed.

The internal implementation of the trim() method is as follows:-

public String trim() {
   String ret = isLatin1() ? StringLatin1.trim(value)
                          : StringUTF16.trim(value);
   return ret == null ? this : ret;
}

Example – Trim String In Java

public class Main {
   public static void main(String args[]) {
      String string = "  Java Programming   ";
      String newString = string.trim();
      System.out.println(newString);
   }
}

Output:-

Java Programming

The original string contains whitespaces. We have called the trim() method on the String object and it removes whitespace from the beginning and end of the string.

Java Trim All Strings In Object

Let us see another example of the trim() method of the Java String class,

public class Main {
   public static void main(String[] args) {
      String str ="   Hello   Programmers   ";
      System.out.println(str.length());
      System.out.println(str);
      String str1 = str.trim();
      System.out.println(str1.length());
      System.out.println(str1);
   }
}

Output:-

25
   Hello   Programmers   
19
Hello   Programmers

In the above program see the difference before using the trim method and after using the trim method. The length of the string before the trim() is 25 and after using trim() is 19 that is it has removed leading and trailing whitespace.

Let us see another example for String.trim() In Java:-

public class Main {

   public static void main(String argvs[]) {
      String string = " java ";
      if ((string.trim()).length() > 0) {
         System.out.println("The string contains characters other "
                           + "than white spaces");
      } else {
         System.out.println("The string contains only white spaces");
      }

      string = "   ";
      if ((string.trim()).length() > 0) {
         System.out.println("The string contains characters other "
                           + "than white spaces");
      } else {
         System.out.println("The string contains only white spaces");
      }
   }
}

Output:-

The string contains characters other than white spaces
The string contains only white spaces

String Trim In Java Example

public class Main {

   public static void main(String argvs[]) {

      String string = " java ";

      String string1 = string.trim();

      System.out.println(string.hashCode());
      System.out.println(string1.hashCode() + "\n");

      String str = "xyz";
      String str1 = str.trim();

      System.out.println(str.hashCode());
      System.out.println(str1.hashCode());

   }
}

Output:-

1017032222
3254818

119193
119193

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *