-2

I have this sample String with Cooking \u0026 Baking Needs \u002A text having Unicodes what is the best way to replace all Unicodes with actual String? Like replace \0026 with & etc.

This is my initial code:

public static String tidy(String s) {
    String unicodeCharRegex = "\\\\u[A-Fa-f\\d]{4}\n";
    if(s != null && !s.isEmpty()) {
        Pattern p = Pattern.compile(unicodeCharRegex);
        Matcher m = p.matcher(s);
        while(m.find()) {

        }
    }
    return s;
}

private String unicodeToString(String u) { 
    // TODO:
    return "";
}
quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

-1

You can use the below code:

private String unicodeToString(String u) { 

String str = u.split(" ")[0];
str = str.replace("\\","");
String[] arr = str.split("u");
String text = "";
for(int i = 1; i < arr.length; i++){
    int hexVal = Integer.parseInt(arr[i], 16);
    text += (char)hexVal;
}

  return text;
}
vikiiii
  • 9,246
  • 9
  • 49
  • 68