Java String trim() Method

❮ String Methods


Example

Remove whitespace from both sides of a string:

String myStr = "       Hello World!       ";
System.out.println(myStr);
System.out.println(myStr.trim());

Try it Yourself »


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
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.