WaterBoy
WaterBoy

Reputation: 709

Removing an invalid escape sequence from a Java String

I have an invalid escape sequence inside a json string. The sequence is:

'ngram': "'s\xa0cancer prevention"

I have been trying to remove this sequence completely by replacing it with a blank string, however each attempt fails. I have tried the following ways:

qumlsOutputAsJson = qumlsOutputAsJson.replaceAll("[^\\x20-\\x7E]", "");

and

qumlsOutputAsJson = qumlsOutputAsJson.replaceAll("\\.", "");

and even a routine:

    private String removeNonAscii(String text){
    String asciiText = "";
    for (char aChar: text.toCharArray()){
        if((int)aChar<=0x7F)
            asciiText = asciiText + Character.toString(aChar);
    }
    return asciiText;
}

All have failed.

I'm sure there is an obvious way, but any direction much appreciated.

Upvotes: 3

Views: 2050

Answers (1)

achAmh&#225;in
achAmh&#225;in

Reputation: 4266

With replaceAll you need to escape the backslash, i.e. "\\\\",""

If you just used replace, yours should work as expected

qumlsOutputAsJson = qumlsOutputAsJson.replaceAll("\\\\", "");

Upvotes: 2

Related Questions