String Trim Method And Example In Java

Hello everyone!! Welcome to flower brackets blog. This post shows string trim method and example in java.

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

Also Read – String Compare Java Program


  • trim() method uses unicode value, that is, \u0020.
  • unicode value is checked before and after a string.
  • trim() method 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.

Example: String Trim Method And Example In Java

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);


String Trim Method And Example In Java

You May Also Like