0

I have an input string that I need to process and hand over to a REST webservice which is not in my control. The webservice doesn't support special characters as part of the JSON-request and throws an error whenever such characters are part of a String.

Is there a way of replacing all characters that are not part of the ASCII char set with their unicode equivalent: \u1005

String myString = "abစcd";
myString.doSomething(); //replace non-ASCII characters
System.out.println(myString) //should print ab\u1005cd

I won't know which special character might be part of myString, and replacing them by an empty string isn't an option either. Is there any way to go around replacing each special character on its own?

edit/solution: The question differs in a way that I want to replace my characters with their unicode equivalent, not delete them / replace them by an empty string.

String input = "    !@#$%^&*()_+£€      "; //contains tabs & different whitespaces
    String returnString = "";
    for(char c:input.toCharArray()){
        if((c | 0x01) > 126){
            returnString += "\\u"+Integer.toHexString(c|0x10000).substring(1);
        }
        else{
            returnString += c;
        }
    }
System.out.println(returnString); //    !@#$%^&*()_+\u00a3\u20ac\u2004\u2005 \u1680\u2002\u2003
  • 2
    This is not a duplicate of http://stackoverflow.com/questions/8519669/replace-non-ascii-character-from-string , as that question is about removing non-ASCII characters altogether, not replacing them with hex escapes. – VGR Jan 26 '17 at 15:34
  • totally agree, this is not a duplicate – Jan Hruby May 04 '17 at 08:48

0 Answers0