In this section, we will learn what the String trim() method is and how to use it in Java.
What is Java String trim() Method?
The `trim()` method is used to remove the white space from both ends of a string value.
Java trim() Method Syntax:
public String trim()
trim() Method Parameters:
The method does not take an argument.
trim() Method Return Value:
The return value of this method is a string with no white space in both sides.
trim() Method Exceptions:
The method does not throw an exception.
Example: using String trim() method
public class Simple {
public static void main(String[] args) {
String s1 = " BE KING TO ONE ANOTHER ";
System.out.println(s1);
System.out.println(s1.trim());
}
}
Output:
BE KING TO ONE ANOTHER
BE KING TO ONE ANOTHER