Java string trim

Hello everyone!! Welcome to flower brackets blog. In this post we are going to learn java string trim.

Java string trim

Java trim() method of String class removes any trailing and leading spaces from a string.

Also Read – String Length Java

Syntax :

public String trim()

Here trim method does not accept parameters.

Returns: 
Trim method returns the string with deleted spaces.


Example: java string trim

public class TrimJava
{
   public static void main(String[] args)
   {
      String strOriginal = " Great Barrier Reef";

      String strTrimmed = strOriginal.trim();
 
      System.out.println("Before using trim() method: " + strOriginal);
 
      System.out.println("Using trim() method: " + strTrimmed);
   }
}

Output :

String trim java
Before : Great Barrier Reef
After : Great Barrier Reef


NOTE :

  • trim() method in java uses unicode value, that is, \u0020.
  • unicode value is checked before and after a string.
  • trim() method in java does not remove middle spaces, that is, for example there is a string “Core Java” so the space between “Core” and “Java” will not be eliminated.

conclusion

This was all about trim method in java. I hope you guys have understood java trim method.

You can subscribe to my blog flower brackets if you haven’t already. Do share this post if you like.

You May Also Like