Java String.trim() Method Example

The String.trim() method returns a string whose value is this string, with any leading and trailing whitespace removed.

Java String.trim() Method Example

This is a complete example to demonstrate the usage of trim()  method.
public class TrimExample {
    public static void main(String[] args) {
        String str = "javaguides ";
        String subStr = str.trim();
        System.out.println("trim the space from given string : " + subStr);

trim the space from given string: javaguides