Search code examples
javautf-8escaping

escaping Java string to utf-8


I'm looking for java tool for converting regular String to utf-8 string.

e.g.

input: special-数据应用-text

output: special-%u6570%u636E%u5E94%u7528-text

(note the preceding "%u")


Solution

  • Try this

    String s= URLEncoder.encode(str, "UTF-8").replaceAll("%(..)%(..)", "%u$1$2");