nemisis enforcer
nemisis enforcer

Reputation: 359

convert unicode in a java string out a string representation of the unicode

I have a string that is this

Temperature: 98.6°F (37.0°C)

Ultimately would like to convert it to look like this

98.6\u00b0F (37.0\u00b0C)

I wind up with all the solutions making this a ? or some other char, what i want to do is put a string for the unicode solution there.

All of the solutions that i have come across or tried don't seem to work.

Thanks in advance.

Upvotes: 1

Views: 1138

Answers (1)

Andreas
Andreas

Reputation: 159185

Just loop through the characters of the string and replace non-ASCII characters with the Unicode escape:

String s = "Temperature: 98.6°F (37.0°C)";
StringBuilder buf = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c >= 0x20 && c <= 0x7E) // visible ASCII character
        buf.append(c);
    else
        buf.append(String.format("\\u%04x", (int) c));
}
String t = buf.toString();
System.out.println(t);

Output

Temperature: 98.6\u00b0F (37.0\u00b0C)

In Java 9+, it's even simpler:

String s = "Temperature: 98.6°F (37.0°C)";
String t = Pattern.compile("[^ -~]").matcher(s)
                  .replaceAll(r -> String.format("\\\\u%04x", (int) r.group().charAt(0)));
System.out.println(t);

Upvotes: 2

Related Questions