Go 字符串比较

简介: Go 字符串比较

golang 字符串比较


字符串比较, 可以直接使用 == 进行比较, 也可用用 strings.Compare 比较

go 中字符串比较有三种方式:

  • == 比较
  • strings.Compare 比较
  • strings.EquslFold 比较


#### 代码示例
```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


忽略大小写比较


有时候要忽略大小写比较, 可以使用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
相关文章
|
4月前
|
安全 Java 编译器
Go 字符串拼接方式
Go 字符串拼接方式
24 0
|
1月前
|
存储 分布式计算 算法
GO学习笔记之表达式
GO学习笔记之表达式
33 1
|
4月前
|
存储 Go 索引
Go 正则表达式
Go 正则表达式
41 0
|
4月前
|
Go
Go 运算符
运算符用于对变量和值执行操作。 加号运算符(+)将两个值相加,如下面的示例所示: 示例代码:
92 2
|
7月前
|
存储 自然语言处理 安全
Go字符串实战操作大全!
Go字符串实战操作大全!
39 0
|
10月前
|
存储 Go
go06 整数
go06 整数
48 0
Golang:go-hashids从整数生成短唯一ID
Golang:go-hashids从整数生成短唯一ID
146 0
|
机器学习/深度学习 Go 数据安全/隐私保护
Go 字符串函数详解(2) | 学习笔记
快速学习 Go 字符串函数详解(2)
61 0
|
Go 数据安全/隐私保护 开发者
Go 字符串函数详解(1) | 学习笔记
快速学习 Go 字符串函数详解(1)
89 0
Go 字符串函数详解(1) | 学习笔记
|
Go 开发者
Go 字符串函数详解(3) | 学习笔记
快速学习 Go 字符串函数详解(3)
100 0