【问题标题】:How to change Unicode char to simple form? [duplicate]如何将 Unicode 字符更改为简单形式? [复制]
【发布时间】:2019-02-09 11:36:59
【问题描述】:

是否有可以将Sjöström 作为输入并返回Sjostrom 作为输出的 Go 库?

【问题讨论】:

    标签: go unicode


    【解决方案1】:

    您可以使用golang.org/x/text/unicode/norm 来处理此问题。

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "strings"
        "unicode"
    
        "golang.org/x/text/transform"
        "golang.org/x/text/unicode/norm"
    )
    
    func main() {
    
        isMn := func(r rune) bool {
            return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
        }
        t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
    
        r := strings.NewReader("Sjöström")
        x := transform.NewReader(r, t)
        b, err := ioutil.ReadAll(x)
        if err != nil {
            panic(err)
        }
    
        fmt.Println(string(b))
    
    }
    

    另请参阅:https://blog.golang.org/normalization

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-07
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      相关资源
      最近更新 更多