Share on Google+Share on Google+

Trim String Example

In this section, you will learn how to remove the blank spaces.

Trim String Example


In this section, you will learn how to remove the blank spaces. For removing  the blank spaces use trim() method that removes the blank spaces and shows only string. 

Description of code:

This method removes the blank spaces from both ends of the given string (Front and End). 

Here is the code of program:



import java.lang.*;

public class StringTrim{
  public static void main(String[] args) {
  System.out.println("String trim example!");
  String str = " RoseIndia";
  System.out.println("Given String :" + str);
  System.out.println("After trim :" +str.trim());

Here is the video tutorial of "How to Trim String in Java?":


Output of program:


C:\vinod\Math_package>java StringTrim
String trim example!
Given String :  RoseIndia
After trim :RoseIndia


Click Download this example.



Posted on: June 8, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Trim String Example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 16, 2012
yes sir!

ahhhhh!!! very good!
January 25, 2012

<p><b><i><a>this is test<p></b></i></a>
March 1, 2012
the right side?

What about when the blank is on the right side?