How do I remove leading white space from a string?

Category: java.lang, viewed: 850 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 leading white spaces from a string.

 
package org.kodejava.example.lang;
 
public class LeadingSpace {
    public static void main(String[] args) {
	String text = "     tattarrattat     ";
 
	//
	// Using a regular expression to remove only the leading 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