【问题标题】:How to decode a string containing backslash-encoded Unicode characters?如何解码包含反斜杠编码的 Unicode 字符的字符串?
【发布时间】:2016-06-01 20:10:48
【问题描述】:

我有一个字符串存储为a:

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

有没有办法可以将a 转换为b

【问题讨论】:

    标签: go escaping unicode-escapes rune


    【解决方案1】:

    您可以为此使用strconv.Unquote

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

    输出:

    München
    

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 2017-06-13
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多