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
相关文章
|
Go C++
【力扣】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
【2月更文挑战第18天】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
106 6
|
并行计算 Go C++
2182.构造限制重复的字符串(模拟 贪心 优先队列 C++ Go)
【2月更文挑战第19天】2182.构造限制重复的字符串(模拟 贪心 优先队列 C++ Go)
95 1
|
安全 Java 编译器
Go 字符串拼接方式
Go 字符串拼接方式
104 0
|
2月前
|
人工智能 自然语言处理 算法
Go语言统计字符串中每个字符出现的次数 — 简易频率分析器
本案例实现一个字符统计程序,支持中文、英文及数字,可统计用户输入文本中各字符的出现次数,并以整洁格式输出。内容涵盖应用场景、知识点讲解、代码实现与拓展练习,适合学习文本分析及Go语言基础编程。
|
供应链 Go C语言
掌握Go语言:探索Go语言基础,字符串连接、关键字、空格、格式化字符串的重要性(7)
掌握Go语言:探索Go语言基础,字符串连接、关键字、空格、格式化字符串的重要性(7)
159 0
|
10月前
|
存储 Go 索引
go语言中遍历字符串
go语言中遍历字符串
158 5
|
5月前
|
Go 索引
【LeetCode 热题100】394:字符串解码(详细解析)(Go语言版)
本文详细解析了 LeetCode 热题 394:字符串解码。题目要求对编码字符串如 `k[encoded_string]` 进行解码,其中 `encoded_string` 需重复 `k` 次。文章提供了两种解法:使用栈模拟和递归 DFS,并附有 Go 语言实现代码。栈解法通过数字栈与字符串栈记录状态,适合迭代;递归解法则利用函数调用处理嵌套结构,代码更简洁。两者时间复杂度均为 O(n),但递归需注意栈深度问题。文章还总结了解题注意事项及适用场景,帮助读者更好地掌握字符串嵌套解析技巧。
141 6
|
6月前
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
188 11
|
9月前
|
Go
go语言for 遍历字符串
go语言for 遍历字符串
128 8
|
10月前
|
Go 索引
go语言遍历字符串
go语言遍历字符串
186 3