Please note, this is a STATIC archive of website www.w3schools.com from 05 May 2020, cach3.com does not collect or store any user information, there is no "phishing" involved.
THE WORLD'S LARGEST WEB DEVELOPER SITE

Java String trim() Method

❮ String Methods


Example

Remove whitespace from both sides of a string:

public class MyClass {
  public static void main(String[] args) {
    String myStr = "       Hello World!       ";
    System.out.println(myStr);
    System.out.println(myStr.trim());
  }
}

Run example »


Definition and Usage

The trim() method removes whitespace from both ends of a string.

Note: This method does not change the original string.


Syntax

public String trim()

Parameters

None.

Technical Details

Returns: A String value, which is a copy of the string, without leading and trailing whitespace

❮ String Methods