As programmers, we come across instances where we need to compare strings. This could be we are checking for the input from the user or a similar task. Whatever the need may be, it is important to know how to perform string comparisons.

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:

  1. == – equality check
  2. ! = – not equal
  3. >= – greater than or equal to
  4. <= – 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:

package main


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:

$ go run string_compare.go

false

It is good to remember that the equality operator checks for case sensitivity. For example, the following comparison returns false:

fmt.Println(“SQL” == “sql”)

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.ToLower(“SQL”) == “sql”)


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:

package main


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.

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:

package main


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:

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:

  1. 0 if the two strings are equal.
  2. 1 if the first string is greater than the second.
  3. -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.

package main


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(“hey”, “hey”))


fmt.Println(strings.Compare(“SQL”, “sql”))

The above snippets should return an output as:

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:

fmt.Println(strings.EqualFold(“SQL”, “sql”))

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!

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/john-150×150.png621352ae8211f.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list