加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ChineseFirstLetter.go 3.54 KB
一键复制 编辑 原始数据 按行查看 历史
老马 提交于 2020-05-12 20:21 . 'mng'
package xjutils
import (
"bytes"
"encoding/hex"
"io/ioutil"
"strconv"
"strings"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// 获取中文字符串第一个首字母
func GetChineseFirstLetter(chinese string) string {
// 获取中文字符串第一个字符
firstChar := string([]rune(chinese)[:1])
if firstChar >= "a" && firstChar <= "z" {
return strings.ToUpper(firstChar)
}
if firstChar >= "A" && firstChar <= "Z" {
return firstChar
}
if firstChar == "(" || firstChar == "[" || firstChar == "(" || firstChar == "【" {
firstChar = string([]rune(chinese)[1:2])
}
// Utf8 转 GBK2312
firstCharGbk, err := Utf8ToGbk([]byte(firstChar))
if err != nil {
return ""
}
// 获取第一个字符的16进制
firstCharHex := hex.EncodeToString(firstCharGbk)
// 16进制转十进制
firstCharDec, err := strconv.ParseInt(firstCharHex, 16, 0)
if err != nil {
return ""
}
// 十进制落在GB 2312的某个拼音区间即为某个字母
firstCharDecimalRelative := firstCharDec - 65536
if firstCharDecimalRelative >= -20319 && firstCharDecimalRelative <= -20284 {
return "A"
}
if firstCharDecimalRelative >= -20283 && firstCharDecimalRelative <= -19776 {
return "B"
}
if firstCharDecimalRelative >= -19775 && firstCharDecimalRelative <= -19219 {
return "C"
}
if firstCharDecimalRelative >= -19218 && firstCharDecimalRelative <= -18711 {
return "D"
}
if firstCharDecimalRelative >= -18710 && firstCharDecimalRelative <= -18527 {
return "E"
}
if firstCharDecimalRelative >= -18526 && firstCharDecimalRelative <= -18240 {
return "F"
}
if firstCharDecimalRelative >= -18239 && firstCharDecimalRelative <= -17923 {
return "G"
}
if firstCharDecimalRelative >= -17922 && firstCharDecimalRelative <= -17418 {
return "H"
}
if firstCharDecimalRelative >= -17417 && firstCharDecimalRelative <= -16475 {
return "J"
}
if firstCharDecimalRelative >= -16474 && firstCharDecimalRelative <= -16213 {
return "K"
}
if firstCharDecimalRelative >= -16212 && firstCharDecimalRelative <= -15641 {
return "L"
}
if firstCharDecimalRelative >= -15640 && firstCharDecimalRelative <= -15166 {
return "M"
}
if firstCharDecimalRelative >= -15165 && firstCharDecimalRelative <= -14923 {
return "N"
}
if firstCharDecimalRelative >= -14922 && firstCharDecimalRelative <= -14915 {
return "O"
}
if firstCharDecimalRelative >= -14914 && firstCharDecimalRelative <= -14631 {
return "P"
}
if firstCharDecimalRelative >= -14630 && firstCharDecimalRelative <= -14150 {
return "Q"
}
if firstCharDecimalRelative >= -14149 && firstCharDecimalRelative <= -14091 {
return "R"
}
if firstCharDecimalRelative >= -14090 && firstCharDecimalRelative <= -13319 {
return "S"
}
if firstCharDecimalRelative >= -13318 && firstCharDecimalRelative <= -12839 {
return "T"
}
if firstCharDecimalRelative >= -12838 && firstCharDecimalRelative <= -12557 {
return "W"
}
if firstCharDecimalRelative >= -12556 && firstCharDecimalRelative <= -11848 {
return "X"
}
if firstCharDecimalRelative >= -11847 && firstCharDecimalRelative <= -11056 {
return "Y"
}
if firstCharDecimalRelative >= -11055 && firstCharDecimalRelative <= -10247 {
return "Z"
}
return SpecialChar(firstChar)
}
func SpecialChar(src string) string {
switch src {
case "亳":
return "B"
case "禧":
return "X"
}
return ""
}
// Utf8ToGbk
func Utf8ToGbk(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化