String Trim Method And Example In Java

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.


  • 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

Also Read – String Compare Java Program

public class Trim_Java {

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

String str_Trimmed = str_Original.trim();
System.out.println("Before using trim() method: " + str_Original);
System.out.println("Using trim() method: " + str_Trimmed);




String Trim Method And Example In Java

About the author


View all posts

Leave a Reply

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