How do I remove trailing white space from a string?

Category: java.lang, viewed: 14093 time(s).

The trim method of a String class removes leading and trailing white space from a string. In this example we use a regular expression to remove only the trailing white spaces from a string.

package org.kodejava.example.lang;

public class TrailingSpace {
    public static void main(String[] args) {
	String text = "     tattarrattat     ";

	//
	// Using a regular expression to remove only the trailing white space in
	// a string
	//
	text = text.replaceAll("\\s+$", "");
	System.out.println("Text: " + text);
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats