Home >>Java String Methods >Java String trim() Method

Java String trim() Method

Java String trim() Method

The trim() method in Java String is used to removes whitespace from both ends of a string.

Syntax

public String trim()

Parameters

None

Returns

It is used to return A String value, which is a copy of the string, without leading and trailing whitespace

Java String trim() Method Example 1


public class MyClass 
{
  public static void main(String[] args) 
  {
    String strln1 = "       Hello Phptpoint!        ";
    System.out.println(strln1);
    System.out.println(strln1.trim());
  }
}

Output:
   Hello Phptpoint!
Hello Phptpoint!

Java String trim() Method Example 2


public class MyClass 
{  
    public static void main(String[] args) 
	{  
        String n1 ="  hello Phptpoint   ";  
        System.out.println(n1.length());  
        System.out.println(n1); 
        String tr = n1.trim();  
        System.out.println(tr.length());  
        System.out.println(tr);  
    }  
}  

Output:
20
 hello Phptpoint
15
hello Phptpoint

No Sidebar ads