Java - removing \u0000 from an String

前端 未结 2 452
情歌与酒
情歌与酒 2020-12-20 18:20

I\'m using the Twitter API and I have the following string that is bugging me Proyecto de ingeniera comercial, actual Profesora de matemáticas \\u0000\\u0000\\u0000\\u

相关标签:
2条回答
  • 2020-12-20 18:53

    The first argument to replaceAll is a regular expression, and the Java regex engine understands \uNNNN escapes so

    json.replaceAll("\\u0000", "")
    

    will search for the regular expression \u0000, which matches instances of the Unicode NUL character (U+0000), not instances of the actual string \u0000. If you want to match the string \u0000 then you need to use the regular expression \\u0000, which in turn means the Java string literal "\\\\u0000"

    json.replaceAll("\\\\u0000", "")
    

    Or more simply, use replace (whose first argument is a literal string rather than a regex) instead of replaceAll

    json.replace("\\u0000", "")
    
    0 讨论(0)
  • 2020-12-20 18:55
    string = string.replace("\u0000", ""); // removes NUL chars
    string = string.replace("\\u0000", ""); // removes backslash+u0000
    

    The character with u-escaping is done on java source level. For instance "class" is:

    public \u0063lass C {
    

    Also you do not need regex.

    0 讨论(0)
提交回复
热议问题