How to Compare Strings in Go

Sheeraz Gul Feb 02, 2024
  1. Use the Compare() Method to Compare Strings in GoLang
  2. Use Comparison Operators to Compare Strings in GoLang
How to Compare Strings in Go

This tutorial demonstrates how to compare strings in GoLang.

GoLang has two methods to compare strings: the built-in method Compare() and the other is a comparison by GoLang comparison operators. This tutorial demonstrates these two methods to compare strings in GoLang.

Use the Compare() Method to Compare Strings in GoLang

The built-in method Compare() in GoLang is used to compare two strings. This method uses the lexicographical order to compare two strings; the syntax for this method is:

func Compare(a, b string) int

Where a and b are the two strings to be compared, it returns an integer value. There are three return types based on the comparison of strings.

  1. If a == b then compare() method will return 0.
  2. If a > b then compare() method will return 1.
  3. If a < b then compare() method will return -1.

Let’s try an example to compare strings using the Compare() method.

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.")
	}
}

The code above tries to compare strings using the Compare() method and returns the integers described above. The code also creates a condition based on the returned integer.

See the output:

-1
1
0
1
-1
The strings do not match.

Use Comparison Operators to Compare Strings in GoLang

GoLang also supports the comparison operators as in other languages; these comparison operators include ==, !=, >=, <=, <, >. The == and != are used to check the equality of strings, and the other four are used to compare the strings in lexicographical order.

These comparison operators return Boolean operators, i.e., true and false. Let’s try an example first using the == and != operators.

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)

}

The code above will compare the given strings based on equality and inequality. See the output:

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

Now let’s try an example using the >=, <=, <, > operators to compare strings by their lexicographical order. See example:

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)

}

The above strings will now be compared based on the lexicographical order. See the output:

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
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Go String