Throughout this article, we will inspect various ways and methods to perform string comparison operations in the go programming language.
Method 1 – Comparison Operators
The most basic form of comparing strings is to use go comparison operators. The following are some comparison operators you can use:
- == – equality check
- ! = – not equal
- >= – greater than or equal to
- <= – less than or equal to
Let us look at a few examples.
Equality Operator
The equality operator allows you to check if the value on the left side equals the one on the right side.
We can use this operator to check if one string is equal to the other.
An example is as shown:
import "fmt"
func main() {
str_1 := "MySQL"
str_2 := "SQL"
fmt.Println(str_1 == str_2)
}
In the example above, we check if the value of the str_1 variable is equal to the value of the str_2 variable.
Since the strings are not equal, the program returns false:
false
It is good to remember that the equality operator checks for case sensitivity. For example, the following comparison returns false:
If you want to perform a case insensitive comparison, you can first convert the strings to lowercase or uppercase (as you see fit) or use the a built-in method (covered later).
The following example shows how to convert the strings to uppercase or lowercase:
fmt.Println(strings.ToUpper("sql") == "SQL")
The above comparisons return true as both strings are equal.
Not Equal Operator
You can also use the not equal operator (!=). It works closely similar to the equality operator, except it returns the negated version of the result. Hence, if two strings are equal, the not equal operator returns false.
An example is as shown:
import "fmt"
func main() {
str_1 := "MySQL"
str_2 := "SQL"
fmt.Println(str_1 != str_2)
}
Here, the two strings are not equal. This means the not equal operator returns true.
true
Lexicographical Operators
You can perform the lexicographical comparison using greater than, less than, greater than or equal to, and less than or equal to. Lexicographical comparison compares if the strings are of equal length and if the characters are in a similar position (alphabetically).
Consider the example code shown below:
import "fmt"
func main() {
fmt.Println("abcd" > "abcd")
fmt.Println("abcd" = "abcd")
fmt.Println("defc" <= "abcd")
}
The above example program performs a lexicographical comparison with the specified strings. The resulting output is as shown:
true
true
false
Method 2 – Compare() Method
The strings package provides us with a built-in method to perform string comparison. The compare() method takes two strings are its arguments.
The function returns:
- 0 if the two strings are equal.
- 1 if the first string is greater than the second.
- -1 if the first string is less than the second.
NOTE: The greatness and less-ness of the strings are determined by a lexicographical comparison.
The example below illustrates how to use the compare() method.
import (
"fmt"
"strings"
)
func main() {
str_1 := "Hello"
str_2 := "Hey"
fmt.Println(strings.Compare(str_1, str_2))
}
The above example returns -1.
We can also pass literal strings to the method as:
fmt.Println(strings.Compare("SQL", "sql"))
The above snippets should return an output as:
0
-1
Method 3 – EqualFold() Method
As you will notice from the above example, the Compare() method is case-sensitive. Hence if two strings do not match cases, it considers them not equal.
However, we may want to perform string comparison without performing a conversion to uppercase or lowercase.
In such a case, we can use the EqualFold() method. It works similarly to the compare() method without the case sensitivity.
Consider the example below:
true
The function returns a Boolean true.
Conclusion
This guide takes you on a tour of using several methods to perform string comparison in the go programming language.
Thanks for reading!