Learn how to compare strings in the golang
Posted on: June 02, 2020 by Shubham
A string value is a sequence of bytes.
In the software developer’s life, comparing strings and sorting it is a day to day routine. In this tutorial, we will learn different ways to compare strings in the golang.
Using the Comparison operators
== equal
!= not equal
< less
<= less or equal
> greater
>= greater or equal
To check if strings are equal or not, you can use ==
or !=
.
package main
import (
"fmt"
)
func main() {
str1 := "golang"
str2 := "gopher"
fmt.Println(str1 == str2)
fmt.Println(str1 != str2)
}
Output
false
true
To check the Lexicographic Order of 2 strings, you can use <
, <=
, >
, >=
.
Lexicographic Order: This is dictionary order, except that all the uppercase letters preceed all the lowercase letters.
Example: UppercaseA
comes first then the lowercasea
.
package main
import (
"fmt"
)
func main() {
str1 := "apple"
str2 := "banana"
str3 := "Apple"
fmt.Printf("%s < %s = %v\n", str1, str2, str1 < str2)
fmt.Printf("%s <= %s = %v\n", str1, str2, str1 <= str2)
fmt.Printf("%s > %s = %v\n", str1, str2, str1 > str2)
fmt.Printf("%s >= %s = %v\n", str1, str2, str1 >= str2)
fmt.Printf("%s < %s = %v\n", str1, str3, str1 < str3)
fmt.Printf("%s > %s = %v\n", str1, str3, str1 > str3)
}
Output
apple < banana = true
apple <= banana = true
apple > banana = false
apple >= banana = false
apple < Apple = false
apple > Apple = true
Note: In Lexicographic Order, Apple comes before apple. ? != ?
Using the Compare function
Compare returns an integer comparing two strings lexicographically.
The result will be
0 if a == b
-1 if a < b
+1 if a > b
Example:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Compare("a", "b"))
fmt.Println(strings.Compare("a", "a"))
fmt.Println(strings.Compare("b", "a"))
}
Output
-1
0
1
Share on social media
//