• 技术文章 >后端开发 >Golang

    go语言怎么比较字符串

    青灯夜游青灯夜游2023-01-14 13:06:28原创65

    比较方法:1、直接使用“==”运算符比较,语法“str1 == str2”,该方法区分大小写。2、利用strings包的Compare()函数比较,语法“strings.Compare(a,b)”;返回值为int类型,0表示两数相等,1表示a大于b,“-1”表示a小于b。3、利用strings包的EqualFold()比较,语法“strings.EqualFold(a,b)”。

    本教程操作环境:windows7系统、GO 1.18版本、Dell G3电脑。

    Go语言比较字符串方式

    在 go 语言中字符串比较的方式有如下三种:

    示例如下:// 1-使用等号比较——区分大消息

    func Equal(s1, s2 string) bool {
    	return s1 == s2
    }
    
    // 2-使用 compare 比较——区分大小写
    func Compare(s1, s2 string) bool {
    	return strings.Compare(s1, s2) == 0 //
    }
    
    
    //3-EqualFold 比较——不区分大小写. case-fold 即大小写同一处理
    func EqualFold(s1, s2 string) bool {
    	return strings.EqualFold(s1, s2)
    }
    
    // 使用等号比较——忽略大小写
    func Equal2(s1, s2 string) bool {
    	return strings.ToLower(s1) == strings.ToLower(s2)
    }
    
    // 使用 compare 比较——不区分大小写
    func Compare2(s1, s2 string) bool {
    	return strings.Compare(strings.ToLower(s1), strings.ToLower(s2)) == 0
    }
    
    
    func StringCompareTest() {
    	fmt.Println("== 区分大小写", Equal("go", "Go")) 	//false
    	fmt.Println("== 忽略大小写",Equal2("go", "Go"))  //true
    	fmt.Println("compare 区分大小写",Compare("go", "Go")) //false
    	fmt.Println("compare 忽略大小写",Compare2("go", "Go")) //true
    	fmt.Println("EqualFold 忽略大小写",EqualFold("go", "Go")) // true
    }

    性能比较

    下面的代码使用 Benchmark 做简单的性能比较,测试项目的目录结构为:

    1.png

    详细代码:

    package test
    
    import (
    	"../str"
    	"testing"
    )
    
    func BenchmarkStrEqual(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		str.Equal("go", "Go")
    	}
    }
    func BenchmarkStrEqual2(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		str.Equal2("go", "Go")
    	}
    }
    func BenchmarkStrCompare(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		str.Compare("go", "Go")
    	}
    }
    func BenchmarkStrCompare2(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		str.Compare2("go", "Go")
    	}
    }
    func BenchmarkStrEqualFold(b *testing.B) {
    	for i := 0; i < b.N; i++ {
    		str.EqualFold("go", "Go")
    	}
    }

    测试结果如下:

    2.png

    通过上图可以看出,效率最高的还是 ==

    源码简单分析

    1、strings.Compare

    package strings
    
    // Compare returns an integer comparing two strings lexicographically.
    // The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
    //
    // 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 {
    	// NOTE(rsc): This function does NOT call the runtime cmpstring function,
    	// because we do not want to provide any performance justification for
    	// using strings.Compare. Basically no one should use strings.Compare.
    	// As the comment above says, it is here only for symmetry with package bytes.
    	// If performance is important, the compiler should be changed to recognize
    	// the pattern so that all code doing three-way comparisons, not just code
    	// using strings.Compare, can benefit.
    	if a == b {
    		return 0
    	}
    	if a < b {
    		return -1
    	}
    	return +1
    }

    如上所示,我们发现,Compare 内部也是调用了 == , 而且该函数的注释中也说了,这个函数 only for symmetry with package bytes。而且推荐我们直接使用 ==><

    2、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
    }

    这个函数中做了一系列操作,将两个字符串转换成 utf-8 字符串进行比较,并且比较时忽略大小写。

    总结

    通过上面的简单总结和分析,我们发现,字符串比较还是直接用 == 、>、 < 比较运算符吧,简单快捷效率高。

    【相关推荐:Go视频教程编程教学

    以上就是go语言怎么比较字符串的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Golang go语言
    上一篇:go语言中数组与切片有什么区别 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • php实现通过JSON RPC与go通讯(附代码)• go语言怎么获取字符串长度• golang中map是什么• golang怎么获取注释内容• go语言怎么实现字符串首字母大写• go语言怎么重复字符串
    1/1

    PHP中文网