0

I am reading data from xml. When I checked in eclipse console I found I am getting the whole data with some square boxes. Example If there is 123 in excel sheet i am getting 123 with some square boxes. I used trim() to avoid such things but didnot get success because trim() method trims only white spaces. But I found those characters have ASCII value -17, -20 .. I dont want to trim only white spaces I want to trim those square boxes also

So I have used the following method to trim those characters and I got success.

What is the more appropriate way of trimming a string

Trimming a string

String trimData(String accessNum){
        StringBuffer sb = new StringBuffer();
        try{
            if((accessNum != null) && (accessNum.length()>0)){
//              Log.i("Settings", accessNum+"Access Number length....."+accessNum.length());
                accessNum = accessNum.trim();
                byte[] b = accessNum.getBytes();
                for(int i=0; i<b.length; i++){
                    System.out.println(i+"....."+b[i]);
                    if(b[i]>0){
                        sb.append((char)(b[i]));
                    }
                }
//              Log.i("Settigs", accessNum+"Trimming....");
            }}catch(Exception ex){

            }
            return sb.toString();
    }

2 Answers2

3

Edited

use Normalizer (since java 6)

public static final Pattern DIACRITICS_AND_FRIENDS 
        = Pattern.compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");


private static String stripDiacritics(String str) {
        str = Normalizer.normalize(str, Normalizer.Form.NFD);
        str = DIACRITICS_AND_FRIENDS.matcher(str).replaceAll("");
        return str;
}

And here and here are complete solution.

And if you only want to remove all non printable characters from a string, use

rawString.replaceAll("[^\\x20-\\x7e]", "")

Ref : replace special characters in string in java and How to remove high-ASCII characters from string like ®, ©, ™ in Java

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

Try this:

  str = (str == null) ? null :
     str.replaceAll("[^\\p{Print}\\p{Space}]", "").trim();
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94