Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 692
Daniele B Avatar asked Dec 24 '22 09:12

Daniele B


1 Answers

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
like image 189
cnicutar Avatar answered Apr 26 '23 02:04

cnicutar