Java String trim() Vs strip()

Java String trim() Vs strip() | In this section, we will discuss the difference between trim() and strip() methods. Both are built-in methods in the Java String class but their functionalities are different.

The trim() method removes the leading and trailing white spaces in the given string whereas the strip() method is Unicode charset that is it removes the white spaces according to the Unicode specified there are three variations in the strip method as listed below:-
1. strip()
2. stripLeading()
3. stripTraling()

The strip() method is used to remove all the white spaces in the string, the stripLeading() method removes all the leading spaces, and the stripTraling() method removes all the spaces that are at the end of the string. Both the functions trim() and strip() are used to remove the white spaces but the major difference is that by using the trim() function we can’t remove all the white spaces but by using strip() we can remove the white spaces wherever required.

Java String trim() Vs strip() Example

Observe the output which shows the difference between trim() and strip() methods.

public class Main {
   public static void main(String[] args) {
      String string1 = '\u2003' + "Java";
      System.out.println("String: " + string1);
      System.out.println("String length: " + string1.length());

      String string2 = string1.trim();
      System.out.println("After using trim() function: " + string2);
      System.out.println("Length: " + string2.length());

      String string3 = string1.strip();
      System.out.println("After using strip() function: " + string3);
      System.out.println("Length: " + string3.length());
   }
}

Output:-

String: ?Java
String length: 5
After using trim() function: ?Java
Length: 5
After using strip() function: Java
Length: 4

From the above example, we can understand that trim() method doesn’t remove the ‘\u2003’ from the beginning of the string whereas the strip() method was able to remove this character from the given string. Let us see what happens if the string contains the normal space character.

Java String trim() Vs strip() Example-2

public class Main {
   public static void main(String[] args) {
      String string1 = "   Java   " + "\u2003\u2003\u2003";
      System.out.println("String: " + string1);
      System.out.println("String length: " + string1.length());

      String string2 = string1.trim();
      System.out.println("After using trim() function: " + string2);
      System.out.println("Length: " + string2.length());

      String string3 = string1.strip();
      System.out.println("After using strip() function: " + string3);
      System.out.println("Length: " + string3.length());
   }
}

Output:-

String: Java ???
String length: 13
After using trim() function: Java ???
Length: 10
After using strip() function: Java
Length: 4

The string starts with space characters and ends with “\u2003\u2003\u2003” but before this, it also contains 3 more spaces. The actual length of the string “Java” is 4. The trim() method removes spaces only at the beginning because string ends with the character ‘\u2003’. But the strip() method removes all whitespace and produces the result “Java”.

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.