Java - removing \u0000 from an String

前端 未结 2 455
情歌与酒
情歌与酒 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", "")
    

提交回复
热议问题