更好地比较片或字节?

更好地比较片或字节?,第1张

更好地比较片或字节

如果用字母表示您的符文,请使用:

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]}

比较单个字节的速度比比较基准测试中所示的字符串切片的速度要快。

游乐场的例子



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5163395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存