Java String trim() Method

trim():

Returns a copy of the string, with leading and trailing white space omitted.

Syntax:

public String trim()

 

Example

class TestString{
    String str = "   www.w3schools.blog  ";
    
    public void trimString(){
        //will remove all leading and trailing whitespace. 
        System.out.println(str.trim());
    }
}

public class StringTrimExample {
    public static void main(String args[]){
        //creating TestString object
        TestString obj = new TestString();
        
        //method call
        obj.trimString();
    }
}

Output

www.w3schools.blog