Top Menu

Custom Search

Tuesday, September 4, 2012

White space character not being removed by java trim()

Java String's trim() functions trims only white spaces defined in ASCII. Others are not trimmed.
and when printing those characters in the string you will find value as 160.
 
        char c = str.charAt(0);
        int val = (int) c;
        System.out.println("The char:"+val+":");
output: 
The char:160:

Unicode equivalent: \\u00A0


So we will need a unicode compatible method for trimming this.

Some indicate a way to do it by using some google library : http://code.google.com/p/guava-libraries/

Custom Solution:

boolean isWhiteSpace (String word) { 
  if(word.replaceAll("\\u00A0","").length() < 1){
            return true;
        }
}

And then you can decide on removing it from processing. 

1 comment:

Swaminathan L said...

Very nice and useful info.
Thanks
Swaminathan, Swaradha Typesetting, New Delhi, Mobile: 9873336142