java - What is the better approach to trim unprintable characters from a string -
i reading data xml. when checked in eclipse console found getting whole data square boxes. example if there 123 in excel sheet getting 123 square boxes. used trim()
avoid such things didnot success because trim() method trims white spaces. found characters have ascii value -17, -20 .. i dont want trim white spaces want trim square boxes also
so have used following method trim characters , got success.
what more appropriate way of trimming string
trimming 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(); }
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 , here complete solution.
and if want remove non printable characters string, use
rawstring.replaceall("[^\\x20-\\x7e]", "")
ref : replace special characters in string in java , how remove high-ascii characters string ®, ©, ™ in java
Comments
Post a Comment