Different ways to compare Strings in Golang

In Go, there are a few different ways to compare strings. Let’s explore the most common methods:

  1. Equality Operator (== and !=): You can use the equality (==) and inequality (!=) operators to compare strings for equality. This comparison is case-sensitive.
str1 := "Hello"
str2 := "hello"
isEqual := str1 == str2 // false

Comparing with strings.Compare(): The strings.Compare() function from the strings package can be used to compare two strings. It returns an integer indicating the lexicographic relationship between the strings. If the return value is 0, the strings are equal.

import "strings"

result := strings.Compare("apple", "banana")
// result < 0: "apple" comes before "banana"

Case-Insensitive Comparison: If you want to perform case-insensitive comparison, you can convert both strings to lowercase (or uppercase) before comparing.

str1 := "Hello"
str2 := "hello"
isEqual := strings.ToLower(str1) == strings.ToLower(str2) // true

Using strings.EqualFold(): The strings.EqualFold() function performs a case-insensitive comparison of two strings and returns true if they are equal, regardless of case.

isEqual := strings.EqualFold("Hello", "hello") // true

Custom Comparison: If you need custom comparison logic, you can iterate through the characters of both strings and compare them manually. This allows you to implement your own rules for comparison.

str1 := "apple"
str2 := "banana"
isEqual := customCompare(str1, str2) // your custom compare function

Remember to choose the comparison method based on your specific needs. Case-sensitivity, Unicode support, and specific comparison rules should guide your choice.

Here’s a full example demonstrating different ways to compare strings in Go:

package main

import (

func main() {
	str1 := "apple"
	str2 := "banana"
	str3 := "Apple"

	// Using ==
	isEqual1 := str1 == str2 // false

	// Using strings.Compare()
	compareResult := strings.Compare(str1, str2)
	compareResultMsg := "less than"
	if compareResult == 0 {
		compareResultMsg = "equal to"
	} else if compareResult > 0 {
		compareResultMsg = "greater than"

	// Using case-insensitive comparison
	isEqualIgnoreCase := strings.EqualFold(str1, str3) // true

	fmt.Println("Using ==:")
	fmt.Printf("\"%s\" is %s \"%s\"\n", str1, map[bool]string{true: "==", false: "!="}[isEqual1], str2)

	fmt.Println("\nUsing strings.Compare():")
	fmt.Printf("\"%s\" is %s \"%s\"\n", str1, compareResultMsg, str2)

	fmt.Println("\nUsing case-insensitive comparison:")
	fmt.Printf("\"%s\" is equal (case-insensitive) to \"%s\"\n", str1, str3)

Output :

Using ==:
"apple" is != "banana"

Using strings.Compare():
"apple" is less than "banana"

Using case-insensitive comparison:
"apple" is equal (case-insensitive) to "Apple"

In this example, we demonstrate three different methods of comparing strings:

  1. Using the == operator for equality comparison.
  2. Using strings.Compare() to compare strings lexicographically.
  3. Using strings.EqualFold() to perform case-insensitive comparison.

These methods give you flexibility in choosing the appropriate way to compare strings based on your specific requirements.