Google+

Thursday, June 9, 2011

How To Remove Extra Spaces From String


public static String LRTrim(String str) {

        str = str.replaceAll("  ", " ");         // replace all double spaces with single Space
        return str.trim();   // remove all LEFT side & Right Side Extra white spaces from the string
    }

2 comments:

Raj said...

Bro, in the above code you can make a change by not using while. Because that will remove the spaces between the words also. Right? So only trim is ok.

Unknown said...

thanks buddy. i know we have to remove while statement.
str = str.replaceAll(" "," ");
use to remove duplicate spaces from the string.


this is just a example.
go through other posts

Google+