fork(2) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.text.Normalizer;
  4. import java.nio.charset.*;
  5. import java.io.*;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. System.out.println(convertToIso88591WithCodepointNames("Hello \uD83D\uDE00 \uD835\uDC2E frappé, naïve, soufflé"));
  12. }
  13.  
  14. public static String convertToIso88591WithCodepointNames(String input) {
  15. // Normalize to replace as much as possible with normal character forms
  16. input = Normalizer.normalize(input, Normalizer.Form.NFKC);
  17.  
  18. // Then substitute as required into the target charset
  19. CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder();
  20. StringBuilder resultBuilder = new StringBuilder();
  21.  
  22. // Iterate through the string by codepoint
  23. for (int i = 0; i < input.length(); ) {
  24. int codePoint = input.codePointAt(i);
  25.  
  26. String utf16chars = Character.toString(codePoint);
  27. if (encoder.canEncode(utf16chars)) {
  28. resultBuilder.append(utf16chars);
  29. } else {
  30. // Character is NOT encodable, replace with its Unicode codepoint name
  31. resultBuilder.append('[');
  32. resultBuilder.append(Character.getName(codePoint));
  33. resultBuilder.append(']');
  34. }
  35.  
  36. i += utf16chars.length();
  37. }
  38. return resultBuilder.toString();
  39. }
  40. }
Success #stdin #stdout 0.15s 56196KB
stdin
Standard input is empty
stdout
Hello [GRINNING FACE] u frappé, naïve, soufflé