Java String trim

Introduction

The trim() method returns a copy of the string and removes any leading and trailing whitespace.

It has this general form:

String trim() 

Here is an example:

String s = "   Hello World    ".trim(); 

This puts the string "Hello World" into s.


public class Main {

  public static void main(String[] args) {
    String s = "  Hello  ";
    System.out.println(">"+s+"<");
    /*from  w  ww  . j ava 2s . c  o m*/
    s = s.trim();
    System.out.println(">"+s+"<");
  }
}



PreviousNext

Related