如果用字母表示您的符文,请使用:
func eqRune(s string) bool { if s == "" { return false // or true if that makes more sense for the app } f, _ := utf8.DepreRuneInString(s) // 2nd return value is rune size. ignore it. l, _ := utf8.DepreLastRuneInString(s) // 2nd return value is rune size. ignore it. if f != l { return false } if f == unipre.ReplacementChar { // First and last are invalid UTF-8. Fallback to // comparing bytes. return s[0] == s[len(s)-1] } return true}
如果您的意思是字节,请使用:
func eqByte(s string) bool { if s == "" { return false // or true if that makes more sense for the app } return s[0] == s[len(s)-1]}
比较单个字节的速度比比较基准测试中所示的字符串切片的速度要快。
游乐场的例子
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)