本篇文章演示如何在 Go 语言中比较字符串。
Compare()
在 Go 语言中使用 Compare() 方法比较字符串
Compare()
func Compare(a, b string) int
其中 a 和 b 是要比较的两个字符串,它返回一个整数值。 根据字符串的比较,有三种返回类型。
a == bcompare()a > bcompare()a < bcompare()
Compare()
package main
import (
"fmt"
"strings"
)
func main() {
var string1 = "x"
var string2 = "y"
var string3 = "Delftstack"
var string4 = "Delftscope"
// string1 < string2 should return -1
fmt.Println(strings.Compare(string1, string2))
// string2 > string1 should return 1
fmt.Println(strings.Compare(string2, string1))
// string1 == string1 should return 0
fmt.Println(strings.Compare(string1, string1))
// string3 > string4 should return 1
fmt.Println(strings.Compare(string3, string4))
// string4 < string3 should return -1
fmt.Println(strings.Compare(string4, string3))
// Let's create a condition
string5 := "Hello this is delftstack.com!"
string6 := "Hello this is DELFTSTACK.COM!"
// using the Compare function
if strings.Compare(string5, string6) == 0 {
fmt.Println("The strings are a match.")
} else {
fmt.Println("The strings do not match.")
}
}
Compare()
查看输出:
-1
1
0
1
-1
The strings do not match.
在 Go 语言中使用比较运算符比较字符串
==!=>=<=<>==!=
==!=
package main
import "fmt"
func main() {
string1 := "Delftsatck"
string2 := "Delft"
string3 := "Delftsatck.com"
string4 := "Delftsatck"
// using == operator
ComparisonResult1 := string1 == string2
ComparisonResult2 := string2 == string3
ComparisonResult3 := string3 == string4
ComparisonResult4 := string1 == string4
fmt.Println("Result 1 is: ", ComparisonResult1)
fmt.Println("Result 2 is: ", ComparisonResult2)
fmt.Println("Result 3 is: ", ComparisonResult3)
fmt.Println("Result 4 is: ", ComparisonResult4)
// using != operator
ComparisonResult5 := string1 != string2
ComparisonResult6 := string2 != string3
ComparisonResult7 := string3 != string4
ComparisonResult8 := string1 != string4
fmt.Println("\nResult 5 is: ", ComparisonResult5)
fmt.Println("Result 6 is: ", ComparisonResult6)
fmt.Println("Result 7 is: ", ComparisonResult7)
fmt.Println("Result 8 is: ", ComparisonResult8)
}
上面的代码将根据相等性和不等性比较给定的字符串。 查看输出:
Result 1 is: false
Result 2 is: false
Result 3 is: false
Result 4 is: true
Result 5 is: true
Result 6 is: true
Result 7 is: true
Result 8 is: false
>=<=<>
package main
import "fmt"
func main() {
string1 := "Delftsatck"
string2 := "Delft"
string3 := "Delftsatck.com"
string4 := "Delftsatck"
// using < operator
ComparisonResult1 := string1 < string2
ComparisonResult2 := string2 < string3
ComparisonResult3 := string3 < string4
ComparisonResult4 := string1 < string4
fmt.Println("Result 1 is: ", ComparisonResult1)
fmt.Println("Result 2 is: ", ComparisonResult2)
fmt.Println("Result 3 is: ", ComparisonResult3)
fmt.Println("Result 4 is: ", ComparisonResult4)
// using > operator
ComparisonResult5 := string1 > string2
ComparisonResult6 := string2 > string3
ComparisonResult7 := string3 > string4
ComparisonResult8 := string1 > string4
fmt.Println("\nResult 5 is: ", ComparisonResult5)
fmt.Println("Result 6 is: ", ComparisonResult6)
fmt.Println("Result 7 is: ", ComparisonResult7)
fmt.Println("Result 8 is: ", ComparisonResult8)
// using >= operator
ComparisonResult9 := string1 >= string2
ComparisonResult10 := string2 >= string3
ComparisonResult11 := string3 >= string4
ComparisonResult12 := string1 >= string4
fmt.Println("\nResult 9 is: ", ComparisonResult9)
fmt.Println("Result 10 is: ", ComparisonResult10)
fmt.Println("Result 11 is: ", ComparisonResult11)
fmt.Println("Result 12 is: ", ComparisonResult12)
// using <= operator
ComparisonResult13 := string1 <= string2
ComparisonResult14 := string2 <= string3
ComparisonResult15 := string3 <= string4
ComparisonResult16 := string1 <= string4
fmt.Println("\nResult 13 is: ", ComparisonResult13)
fmt.Println("Result 14 is: ", ComparisonResult14)
fmt.Println("Result 15 is: ", ComparisonResult15)
fmt.Println("Result 16 is: ", ComparisonResult16)
}
现在将根据字典顺序比较上述字符串。 查看输出:
Result 1 is: false
Result 2 is: true
Result 3 is: false
Result 4 is: false
Result 5 is: true
Result 6 is: false
Result 7 is: true
Result 8 is: false
Result 9 is: true
Result 10 is: false
Result 11 is: true
Result 12 is: true
Result 13 is: false
Result 14 is: true
Result 15 is: false
Result 16 is: true