GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java String trim() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java String trim() Method

Last Updated : 1 Jun, 2026

The trim() method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is "\u0020". It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves the original string unchanged.

Example:

Below is an example that demonstrates the usage of the trim() method in Java.

Java
// Java program to demonstrate the 
// usage of the trim() method
public class Trim {
  
    public static void main(String[] args) {
      
        String s1 = "   Hello, Geeks   ";
        
        // Using trim() to remove 
        // leading and trailing whitespaces
        String s2 = s1.trim();
        
        System.out.println("Original String: '" + s1 + "'");
        System.out.println("Trimmed String: '" + s2 + "'");
    }
}

Syntax of trim() Method

String trimmedString = str.trim();

  • Parameter: The trim() method accepts no parameters. 
  • Return Type: It returns a new string with leading and trailing whitespace removed. If no whitespace exists, it returns the original string unchanged.

Examples of trim() in Java

Below are the some examples of trim() in Java

1. Using trim() to Remove Leading and Trailing Whitespaces

Here, we will demonstrate how the trim() method works by removing whitespaces from the beginning and end of the string.

Java
// Java program to demonstrate working
// of java string trim() method
import java.io.*;

class GFG {
    
    public static void main (String[] args) {
      
          // Three strings declared
          String x = "geeks ";
          String y = "for ";
          String z = " geeks";
      
          // Printing without trim function
          System.out.println(x + y + z);
      
          // Using trim function to get result
          System.out.println(x.trim() + y.trim() + z.trim());
    }
}

Explanation: In this example, the trim() method is used to remove the starting and ending spaces from each string before concatenating them.

2. Use trim() that Does Not Modify the Original String

The trim() method returns the string rather than making changes to the original string.

Example:

Java
// Java program to demonstrate working
// of java string trim() method
public class Trim {
  
    public static void main(String args[]) {
      
        // String with leading and trailing spaces
        String s = "  Geeks for Geeks  ";

        // Printing String after removing the whitespaces
        System.out.println(s.trim());

        // Printing string to observe original string
        System.out.println(s);
    }
}

Explanation: As we can see, the trim() method returns a new string with the whitespaces removed, but the original string "s" remains unchanged.

3. Use trim() to Compare the Original and Trimmed Strings

When we use the trim() method, we get the original and trimmed string. Both are different, in case we are removing the whitespaces from the original string.

Example:

Java
// Java program to demonstrate working
// of java string trim() method
public class Trim {
  
    public static void main(String[] args) {
      
        // String with leading and trailing spaces
        String s1 = "   Geeks For Geeks   ";

        // Before Trim() method
        System.out.println("String 1: " + s1);
        System.out.println("Length: " + s1.length());

        // Applying trim() method on string s1
        String s2 = s1.trim();

        // After Trim() method
        System.out.println("\nString 2: " + s2);
        System.out.println("Length: " + s2.length());

        // Comparing both the strings
        if (s1 == s2) {
            System.out.println("\nEqual");
        } else {
            System.out.println("\nNot Equal");
        }
    }
}

Explanation: In this example, the trim() method creates a new string without modifying the original string. As a result, comparing the original string s1 and the trimmed string s2 shows that they are not the same.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.