dpecb06062 2019-02-09 11:36
浏览 53
已采纳

如何将Unicode char更改为简单形式? [重复]

This question already has an answer here:

Is there a Go library that can take Sjöström as input and return Sjostrom as output?

</div>
  • 写回答

1条回答 默认 最新

  • dongpei3245 2019-02-09 12:39
    关注

    You can use golang.org/x/text/unicode/norm to handle this.

    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))
    
    }
    

    See also: https://blog.golang.org/normalization

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏