<blockquote><p>Comparison method: 1. Directly use the "==" operator to compare, the syntax is "str1 == str2", this method is case-sensitive. 2. Compare using the Compare() function of the strings package, with the syntax "strings.Compare(a,b)"; the return value is int type, 0 means the two numbers are equal, 1 means a is greater than b, and "-1" means a is less than b. . 3. Use the EqualFold() comparison of the strings package, with the syntax "strings.EqualFold(a,b)". </p></blockquote>
<p><img src="https://img.php.cn/upload/article/000/000/024/63c224119dee6813.jpg" alt="How to compare strings in go language" ></p>
<p>The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer. </p>
<h2><strong>Go language string comparison methods</strong></h2>
<p>There are three string comparison methods in go language: </p>
<ul class="ul-level-0">
<li>
<code> ==</code> Direct comparison, case sensitive</li>
<li>
<code>strings.Compare(a,b)</code> The return value of this function is int, 0 means the two numbers are equal, 1 means a>b, -1 means a<b. case-sensitive><li>
<code>strings.EqualFold(a,b)</code> Directly returns whether they are equal, not case-sensitive. </li></b.>
</li>
</ul>
<p>Examples are as follows:<span style="background-color: rgb(248, 248, 248);">// 1-Use equal sign comparison - distinguish large messages</span></p>
<div class="developer-code-block"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">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
}</pre><div class="contentsignin">Copy after login</div></div></div><h2><strong>Performance comparison</strong></h2><p>The following code uses Benchmark to do a simple performance comparison. The directory structure of the test project is: </p><p><img src="https://img.php.cn/upload/image/916/800/564/167367265364778How to compare strings in go language" title="167367265364778How to compare strings in go language" alt="How to compare strings in go language"/></p><p>Detailed code: </p><div class="developer-code-block"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">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")
}
}</pre><div class="contentsignin">Copy after login</div></div></div><p>The test results are as follows:</p><p><img src="https://img.php.cn/upload/image/725/103/124/1673672666716944.png" title="1673672666716944.png" alt="How to compare strings in go language"/></p><p>As can be seen from the above figure, the most efficient one is ==</p><h2><strong>The source code is simple Analysis</strong></h2><h4 id="3.1-strings.Compare" name="3.1-strings.Compare"><strong>1, strings.Compare</strong></h4><div class="developer-code-block"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">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
}</pre><div class="contentsignin">Copy after login</div></div></div><p>As shown above, we found that Compare also calls <code>= internally =</code>, and the comment of this function also says that this function is only for symmetry with package bytes. And it is recommended that we use <code>==</code> and <code>></code>, <code><</code> directly. </p><h4 id="3.2-strings.EqualFold" name="3.2-strings.EqualFold"><strong>2, strings.EqualFold</strong></h4><div class="developer-code-block"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">// 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
}</pre><div class="contentsignin">Copy after login</div></div></div>
<p>This function performs a series of operations to convert two strings into <code>utf -8</code> Strings are compared, ignoring case when comparing. </p>
<h2><strong>Summary</strong></h2>
<p>Through the above simple summary and analysis, we found that for string comparison, it is better to directly use the ==, >, </p>
<p>【Related recommendations: <a href="http://www.php.cn/course/list/44.html" target="_blank">Go video tutorial</a>, <a href="https://www.php.cn/course.html" target="_blank" textvalue="编程教学">Programming teaching</a>】</p>
The above is the detailed content of How to compare strings in go language. For more information, please follow other related articles on the PHP Chinese website!