Golang Compare String PROPERLY [3 Methods]


Written by - Tuan Nguyen
Reviewed by - Deepak Prasad

Different methods to compare strings in GO

In Go language, In Go, a string is in effect a read-only slice of bytes. It’s important to state right up front that a string holds arbitrary bytes. It is not required to hold Unicode text, UTF-8 text, or any other predefined format. As far as the content of a string is concerned, it is exactly equivalent to a slice of bytes. In golang we can compare strings using following ways:

1. Using comparison operators: Go strings support comparison operators, i.e, ==, !=, >=, <=, <, >. Here, the == and != operator are used to check if the given strings are equal or not, and >=, <=, <, > operators are used to find the lexical order. The results of these operators are of the Boolean type, which means that if the condition is met, it will return true; otherwise, it will return false.

2. Using Compare() method: Golang has a built-in string function called Compare() that we can use to compare two strings. This function returns an integer value after comparing two strings lexicographically. Syntax:

func Compare(a, b string) int

The return values are:

  • Return 0, if str1 == str2.
  • Return 1, if str1 > str2.
  • Return -1, if str1 < str2.

3. Using EqualFold() method: strings.EqualFold() Function in Golang reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity. func EqualFold(s1, s2 string) bool: Here, s1 and s2 are strings. It returns the boolean value.

 

Method 1: Using comparison operators

Example 1: Using == and != operator

Let us consider the following example:

// Go program using == and != to compare string
package main

import "fmt"

// Main function
func main() {

	// initializing strings
	str1 := "GoLinuxCloud"
	str2 := "GoLinux"
	str3 := "Golinuxcloud"
	str4 := "GoLinux"

	// using == operation to check strings are equal or not
	compare12 := str1 == str2
	compare13 := str1 == str3
	compare14 := str1 == str4
	compare23 := str2 == str3
	compare24 := str2 == str4
	compare34 := str3 == str4

	fmt.Println("Compare string 1, 2: ", compare12)
	fmt.Println("Compare string 1,3: ", compare13)
	fmt.Println("Compare string 1, 4: ", compare14)
	fmt.Println("Compare string 1, 2: ", compare23)
	fmt.Println("Compare string 1,3: ", compare24)
	fmt.Println("Compare string 1, 4: ", compare34)

	// using == operation to check strings are equal or not
	compareNot12 := str1 != str2
	compareNot13 := str1 != str3
	compareNot14 := str1 != str4
	compareNot23 := str2 != str3
	compareNot24 := str2 != str4
	compareNot34 := str3 != str4

	fmt.Println("\nCompare not equal string 1, 2: ", compareNot12)
	fmt.Println("Compare not equal string 1,3: ", compareNot13)
	fmt.Println("Compare not equal string 1, 4: ", compareNot14)
	fmt.Println("Compare not equal string 1, 2: ", compareNot23)
	fmt.Println("Compare not equal string 1,3: ", compareNot24)
	fmt.Println("Compare not equal string 1, 4: ", compareNot34)

}

Output:

Compare string 1, 2:  false
Compare string 1,3:  false
Compare string 1, 4:  false
Compare string 1, 2:  false
Compare string 1,3:  true
Compare string 1, 4:  false

Compare not equal string 1, 2:  true
Compare not equal string 1,3:  true
Compare not equal string 1, 4:  true
Compare not equal string 1, 2:  true
Compare not equal string 1,3:  false
Compare not equal string 1, 4:  true

 

Example 2: Using == and >=, <= operator

package main

import "fmt"

// Main function
func main() {

	// initializing a slice of string
	myslice := []string{"Abc",
		"Linux", "Golinux", "Forum"}

	testString := "Golinux"

	fmt.Println("Slice: ", myslice)

	// Using comparison operator
	result1 := testString < myslice[0]
	fmt.Println("Result 1: ", result1)

	result2 := testString > myslice[1]
	fmt.Println("Result 2: ", result2)

	result3 := testString <= myslice[2]
	fmt.Println("Result 3: ", result3)

	result4 := testString > myslice[3]
	fmt.Println("Result 4: ", result4)
}

Output:

Slice:  [Abc Linux Golinux Forum]
Result 1:  false
Result 2:  false
Result 3:  true
Result 4:  true
NOTE:
These operators are used to find the lexical order

 

Method 2: Using Compare() function

package main

import (
	"fmt"
	"strings"
)

// Main function
func main() {

	// initializing a slice of string
	myslice := []string{"Abc",
		"Linux", "Golinux", "Forum", "GoLinux"}

	testString := "GoLinux"

	fmt.Println("Slice: ", myslice)

	// Using comparison function
	for _, v := range myslice {
		fmt.Println("Compare GoLinux with", v)
		fmt.Println("Result: ", strings.Compare(testString, v))
		fmt.Println("------")
	}
}

Output: 

Slice:  [Abc Linux Golinux Forum Golinux]
Compare GoLinux with Abc
Result:  1
------
Compare GoLinux with Linux
Result:  -1
------
Compare GoLinux with Golinux
Result:  -1
------
Compare GoLinux with Forum
Result:  1
------
Compare GoLinux with GoLinux
Result:  0
------

 

Method 3: EqualFold() Method

func EqualFold(s, t string) bool: EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity. The function returns a Boolean true if they are equal.

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.

package main

import (
	"fmt"
	"strings"
)

// Main function
func main() {

	// initializing a slice of string
	myslice := []string{"GOLINUX", "GoLiNux", "Golinux"}

	testString := "GoLinux"

	fmt.Println("Slice: ", myslice)

	// Using comparison function
	for _, v := range myslice {
		fmt.Println("Compare GoLinux with", v)
		fmt.Println("Result: ", strings.EqualFold(testString, v))
		fmt.Println("------")
	}
}

Output:

Compare GoLinux with GOLINUX
Result:  true
------
Compare GoLinux with GoLiNux
Result:  true
------
Compare GoLinux with Golinux
Result:  true
------

 

Summary

This guide walks you through the various methods for performing string comparison in the go programming language with different examples. Note that if you were working with Unicode characters, the result could be quite different. 

 

References

https://go.dev/blog/strings
https://pkg.go.dev/unicode/utf8#DecodeRuneInString
https://pkg.go.dev/strings#EqualFold

 

Views: 2

Tuan Nguyen

He is proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise spanning these technologies, he develops robust solutions and implements efficient data processing and management strategies across various projects and platforms. You can connect with him on LinkedIn.

Categories GO

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment