public static String replaceWrongCharsDueToAscii8bit(final String value) {
StringBuilder newValue = new StringBuilder();
for (String chr : value.split("")) {
byte[] bytes = chr.getBytes();
// " ".getBytes() => [194, 160], but must be " ".getBytes() => [32]
if (bytes.length == 2 && bytes[0] == (byte) 194 && bytes[1] == (byte) 160)
chr = " ";
// "×".getBytes() => [195, 151], convert to "*".getBytes() => [42]
if (bytes.length == 2 && bytes[0] == (byte) 195 && bytes[1] == (byte) 151)
chr = "*";
// "×".getBytes() => [-61, -105], convert to "*".getBytes() => [42]
if (bytes.length == 2 && bytes[0] == (byte) -61 && bytes[1] == (byte) -105)
chr = "*";
// "∗".getBytes() => [226, 136, 151], convert to "*".getBytes() => [42]
if (bytes.length == 3 && bytes[0] == (byte) 226 && bytes[1] == (byte) 136 && bytes[2] == (byte) 151)
chr = "*";
// "−".getBytes() => [226, 136, 146], convert to "-".getBytes() => [45]
if (bytes.length == 3 && bytes[0] == (byte) 226 && bytes[1] == (byte) 136 && bytes[2] == (byte) 146)
chr = "-";
// "−".getBytes() => [-30, -120, -110], convert to "-".getBytes() => [45]
if (bytes.length == 3 && bytes[0] == (byte) -30 && bytes[1] == (byte) -120 && bytes[2] == (byte) -110)
chr = "-";
// "".getBytes() => [-30, -128, -117], convert to "".getBytes => []
if (bytes.length == 3 && bytes[0] == (byte) -30 && bytes[1] == (byte) -128 && bytes[2] == (byte) -117)
chr = "";
// any strange chars with byte codes like => [254...255], convert to ""
if (bytes.length == 1 && (bytes[0] == (byte) 254 || bytes[0] == (byte) 255))
chr = "";
// add new byte code if necessary
newValue.append(chr);
}
return newValue.toString();
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter