2022
我们一起努力

Go如何实现字符串比较 - 开发技术

这篇“Go如何实现字符串比较”文章,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要参考一下,对于“Go如何实现字符串比较”,小编整理了以下知识点,请大家跟着小编的步伐一步一步的慢慢理解,接下来就让我们进入主题吧。

#### 代码示例
```go
fmt.Println("go"=="go")
fmt.Println("GO"=="go")

fmt.Println(strings.Compare("GO","go"))
fmt.Println(strings.Compare("go","go"))

fmt.Println(strings.EqualFold("GO","go"))

上述代码执行结果如下:

true
false
-1
0
true

Compare 和 EqualFold 区别

EqualFold 是比较UTF-8编码在小写的条件下是否相等,不区分大小写

// EqualFold reports whether s and t, interpreted as UTF-8 strings, // are equal under Unicode case-folding. func EqualFold(s, t string) bool

要注意的是 Compare 函数是区分大小写的, == 速度执行更快

// Compare is included only for symmetry with package bytes. // It is usually clearer and always faster to use the built-in // string comparison operators ==, <, >, and so on. func Compare(a, b string) int

Go如何实现字符串比较 - 开发技术

忽略大小写比较

有时候要忽略大小写比较, 可以使用strings.EqualFold 字符串比较是否相等
源码实现

// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding, which is a more general
// form of case-insensitivity.
func EqualFold(s, t string) bool {
    for s != "" && t != "" {
        // Extract first rune from each string.
        var sr, tr rune
        if s[0] < utf8.RuneSelf {
            sr, s = rune(s[0]), s[1:]
        } else {
            r, size := utf8.DecodeRuneInString(s)
            sr, s = r, s[size:]
        }
        if t[0] < utf8.RuneSelf {
            tr, t = rune(t[0]), t[1:]
        } else {
            r, size := utf8.DecodeRuneInString(t)
            tr, t = r, t[size:]
        }

        // If they match, keep going; if not, return false.

        // Easy case.
        if tr == sr {
            continue
        }

        // Make sr < tr to simplify what follows.
        if tr < sr {
            tr, sr = sr, tr
        }
        // Fast check for ASCII.
        if tr < utf8.RuneSelf {
            // ASCII only, sr/tr must be upper/lower case
            if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
                continue
            }
            return false
        }

        // General case. SimpleFold(x) returns the next equivalent rune > x
        // or wraps around to smaller values.
        r := unicode.SimpleFold(sr)
        for r != sr && r < tr {
            r = unicode.SimpleFold(r)
        }
        if r == tr {
            continue
        }
        return false
    }

    // One string is empty. Are both?
    return s == t
}

通过源码可看到 if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A'  可以看到不区分大小写的实现。
看个完整测试代码:

// Golang program to illustrate the
// strings.EqualFold() Function
package main

// importing fmt and strings
import (
    "fmt"
    "strings"
)

// calling main method
func main() {
    // case insensitive comparing and returns true.
    fmt.Println(strings.EqualFold("Geeks", "Geeks"))

    // case insensitive comparing and returns true.
    fmt.Println(strings.EqualFold("computerscience", "computerscience"))
}

执行结构
true
true

以上是“Go如何实现字符串比较”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注云行业资讯频道!

赞(0)
文章名称:《Go如何实现字符串比较 - 开发技术》
文章链接:https://www.fzvps.com/78310.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!