// GetStrLength 返回输入的字符串的字数,汉字、中文标点、英文和其他字符都算 1 个字数 func GetStrLength(str string) float64 { var total float64 reg := regexp.MustCompile("/·|,|。|《|》|‘|’|”|“|;|:|【|】|?|(|)|、/") for _, r := range str { if unicode.Is(unicode.Scripts["Han"], r) || reg.Match([]byte(string(r))) { total = total + 1 } else { total = total + 1 } } return math.Ceil(total) } // GetStrLength 返回输入的字符串的字数,汉字、中文标点、英文和其他字符都算 1 个字数,emoji 表情 2 个字数 func GetStrLength(str string) float64 { var total float64 ret := "" rs := []rune(str) for i := 0; i < len(rs); i++ { if len(string(rs[i])) == 4 { u := `[\u` + strconv.FormatInt(int64(rs[i]), 16) + `]` ret += u total += 2 } else { ret += string(rs[i]) total += 1 } } return math.Ceil(total) }