Connect with one of the following accounts


Please wait while we redirect...

Views: 396   

In java java.lang.String Class has a utility metod trim() which removes leading and trailing whitespaces from a String and returns a new String.
Following code shows you how to use trim() method to remove spaces at beginning and at ending from a String

public class TrimWhiteSpaces {

    public static void main(String[] args) {

        String stringWithSpaces = " foo boo     ";
        System.out.println("|" + stringWithSpaces + "|");

        // Trim spaces
        String stringWithoutSpaces = stringWithSpaces.trim();
        System.out.println("|" + stringWithoutSpaces + "|");



| foo boo     |
|foo boo|

On By

Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News