Remove the leading and trailing whitespace of string.

Remove the leading and trailing whitespace of string.


Posted in : Core Java Posted on : November 9, 2010 at 4:40 PM Comments : [ 0 ]

In this example, you will see how to remove the leading and trailing whitespace of string.

Remove the leading and trailing whitespace of string.

In this example, you will see how to remove the leading and trailing whitespace of string. The trim() is a method of String class. It returns a copy of given string, after removing leading and trailing whitespace.

Code:
TrimString.java
public class TrimString {
   public static void main(String[] args) {
	// Create a string.
	String str = "  The trim is a method of String class.  ";
	System.out.println("String : \n" + str);
	// Remove leading and trailing whitespace.
	String strTrim = str.trim();
	System.out
		.println("String after removing leading and trailing whitespace : ");
	System.out.println(strTrim);
    }
}
Output:
String :
  The trim is a method of String class.  
String after removing leading and trailing whitespace :
The trim is a method of String class.

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics