How to decode a string containing backslash-encoded Unicode characters?

不羁的心 提交于 2019-12-10 15:59:30

问题


I have a string stored as a:

a := `M\u00fcnchen`
fmt.Println(a)  // prints "M\u00fcnchen"
b := "M\u00fcnchen"
fmt.Println(b)  // prints "München"

Is there a way I can convert a into b ?


回答1:


You can use strconv.Unquote for this:

u := `M\u00fcnchen`
s, err := strconv.Unquote(`"` + u + `"`)
if err != nil {
    // ..
}
fmt.Printf("%v\n", s)

Outputs:

München


来源:https://stackoverflow.com/questions/35519106/how-to-decode-a-string-containing-backslash-encoded-unicode-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!