Java String trim() method with Example
The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is ‘\u0020’. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string. The trim() method also helps in trimming the characters in Java.
Note: The trim() method doesn’t eliminate middle spaces.
public String trim()
Parameters: The trim() method accepts no parameters.
Return Type: The return type of trim() method is String. It returns the omitted string with no leading and trailing spaces.
Below are examples to show the working of the string trim() method in Java.
geeks for geeks has all java functions to read Chetna loves reading books
Before Trim() - String - Geeks For Geeks Length - 21 After Trim() - String - Geeks For Geeks Length - 15