func StrToUnicode(str string) (string) {
DD := []rune(str) //需要分割的字符串内容,将它转为字符,然后取长度。
finallStr := ""
for i := 0; i < len(DD); i++ {
if unicode.Is(unicode.Scripts["Han"], DD[i]) {
textQuoted := strconv.QuoteToASCII(string(DD[i]))
finallStr += textQuoted[1 : len(textQuoted)-1]
} else {
h := fmt.Sprintf("%x",DD[i])
finallStr += "\\u" + isFullFour(h)
}
}
return finallStr
}
func isFullFour(str string) (string) {
if len(str) == 1 {
str = "000" + str
} else if len(str) == 2 {
str = "00" + str
} else if len(str) == 3 {
str = "0" + str
}
return str
}
func main() {
uncodeStr := StrToUnicode("十大户¥@!#%……&……*()——+《》、,。、;‘、配【】")
fmt.Println(uncodeStr)
}
Golang将字符串转为unicode码。中英文符号亦可转
最新推荐文章于 2024-05-11 14:33:08 发布