I want to remove all non-printable ascii characters from a string while keeping invisible. I thought this would work because spaces, \ n \ r are invisible characters, but not printable? Basically I get a byte array with characters in it, and I don't want them to be in it. So I am trying to convert it to a string, remove characters before using it as a byte array again.
Everything works fine in my code now, but now \ r and \ n are not working. What will be the correct regular expression to save them? Or is there a better way I do it?
public void write(byte[] bytes, int offset, int count) { try { String str = new String(bytes, "ASCII"); str2 = str.replaceAll("[^\\p{Print}\\t\\n]", ""); GraphicsTerminalActivity.sendOverSerial(str2.getBytes("ASCII")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return; } }
EDIT: I tried [^ \ x00- \ x7F], which is an ascii character range .... but then the characters still pass, weird.
source share