How do I remove trailing white space from a string?

Category: java.lang, viewed: 959 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);
    }
}
 
 
Bookmark this example!  

Most Viewed Examples

Google

100 Top & Latest


eXTReMe Tracker
visitor stats