美文网首页golang
golang 中文转拼音/区首字母大写

golang 中文转拼音/区首字母大写

作者: 顶尖少爷 | 来源:发表于2021-04-26 10:25 被阅读0次

    中文转拼音

    go get -u github.com/mozillazg/go-pinyin
    

    代码

    package main
    
    import (
        "bytes"
        "example/pholcus/common/pinyin"
        "fmt"
    )
    
    func main(){
        hans := "中国人-你好"
    
        // 默认
        a := pinyin.NewArgs()
        rows:=pinyin.Pinyin(hans, a)
        fmt.Println(rows)
        strResult:=""
        for i:=0;i<len(rows);i++{
            if len(rows[i])!=0{
                str:=rows[i][0]
                pi:=str[0:1]
                strResult+=string(bytes.ToUpper([]byte(pi)))
            }
        }
        fmt.Println(strResult)
    }
    
    

    执行

    go run main.go
    
    ZGRNH
    

    相关文章

      网友评论

        本文标题:golang 中文转拼音/区首字母大写

        本文链接:https://www.haomeiwen.com/subject/cpisrltx.html