Java - String trim() Method
The Java string trim() method returns a string which is trimmed version of the specified string. It removes whitespaces from both end of the specified string.
Syntax
public String trim()
Parameters
No parameter is required.
Return Value
Returns the trimmed version of the specified string.
Exception
NA.
Example:
In the example below, trim() method returns the trimmed version of the string called MyString.
import java.lang.*; public class MyClass { public static void main(String[] args) { String MyString = " Hello World!. "; String NewString = MyString.trim(); System.out.println(NewString); } }
The output of the above code will be:
Hello World!.
❮ Java.lang - String