Java Data Type Tutorial - Java String.trim()








Syntax

String.trim() has the following syntax.

public String trim()

Example

In the following code shows how to use String.trim() method.

/*from  w ww  . j  av a 2s .co m*/
public class Main {
  public static void main(String[] argv) {
    String str = "     java 2 s.com    ";
    System.out.println(">"+str+"<");
    str = str.trim();
    System.out.println(">"+str+"<");
  }
}
  

The code above generates the following result.