[JAVA] Remove ASCII code control characters from strings

LT;DR

After converting Hex to a byte array and `new String (byte [])`, 0x00 or 0x01 came in and it became mukey, so I checked it.

Remove ASCII control characters from the string.

replaseAsciiCC.java


//Required to convert from hex to byte.
import javax.xml.bind.DatatypeConverter;

//Abbreviated as class definition or method definition

String hex = "0100313233343536373839302D5E5C";  //The first 4 bytes are control characters

byte[] hexFromByte = DatatypeConverter.parseHexBinary(hex);
String strFromByte = new String(hexFromByte);

String resultStr strFromByte.replaceAll("\\p{C}", ""));

//Control characters disappear"1234567890-^\"Should be output.
System.out.println(resultStr);

This is the reason why "\ p {C}" is fine. https://regular-expressions.mobi/unicode.html?wlr=1

Note that the line feed code is also removed. I don't know if it can be used in languages other than Java.

Recommended Posts

Remove ASCII code control characters from strings
[Java] Remove whitespace from character strings
Extract characters from Ruby strings slice method