Go - Compare Strings



In Golang, you can compare strings using different approaches. You can use relational operators (==, !=, <, >) for direct comparisons, the strings.Compare() function for lexicographic comparison, or the strings.EqualFold() function for case-insensitive matching. Each method helps in different scenarios, whether you need a simple equality check or a more flexible comparison.

Comparing Strings Using strings.Compare() function

You can compare two strings in Go using the strings.Compare() function, which returns:

  • 0 if both strings are equal
  • -1 if the first string is smaller
  • 1 if the first string is larger

Example

In this example, we use 'strings.Compare' to compare various strings (a1, a2, a3, a4). It prints the comparison result as -1 if the first string is less than the second, 1 if it's greater, and 0 if they are equal.

package main
// importing fmt and strings
import (
   "fmt"
   "strings"
)
func main() {
   // Initializing the variables
   var a1 = "a"
   var a2 = "b"
   var a3 = "welcome"
   var a4 = "Golang"
   
   // using the Compare function
   // a1 < a2; it will return -1
   fmt.Println(strings.Compare(a1, a2))
   
   // a2 > a1; it will return 1
   fmt.Println(strings.Compare(a2, a1))
   
   // a3 > a4; it will return 1
   fmt.Println(strings.Compare(a3, a4))
   
   // a4 < a3; it will return -1
   fmt.Println(strings.Compare(a4, a3))
   
   // a1 == a1; it will return 0
   fmt.Println(strings.Compare(a1, a1))
}

Output

Let us consider the following example −

-1
1
1
-1
0

Comparing Strings with Conditional Statements

You can compare strings in Go using conditional statements like if, which checks if one string is equal to, greater than, or less than another.

Example

In this example, we are going to use an if condition along with the Compare() function to check whether the two strings are the same or not.

package main
import (
   "fmt"
   "strings"
)
func main() {
   // Initializing the variables
   A := "Golang on Tutorialspoint"
   B := "Golang on Tutorialspoint"
   
   // using the Compare function
   if strings.Compare(A, B) == 0 {
      fmt.Println("Both the strings match.")
   } else {
      fmt.Println("The strings do not match.")
   }
}

Output

As both the strings are equal, the program will generate the following output −

Both the strings match.

Comparing Strings using strings.EqualFold() Function

You can compare strings in Go using the strings.EqualFold() function, which checks if two strings are equal, ignoring case differences.

Example

In this example, we use the strings.EqualFold function to compare two strings, str1 and str2, without considering their case. It prints a message indicating whether the strings are equal or not.

package main

import (
   "fmt"
   "strings"
)

func main() {
   str1 := "GoLang"
   str2 := "golang"
   
   if strings.EqualFold(str1, str2) {
      fmt.Println("Both strings are equal (case-insensitive).")
   } else {
      fmt.Println("Strings are not equal (case-insensitive).")
   }
}

Output

It will generate the following output −

Both strings are equal (case-insensitive).

Comparing Substrings

You can compare substrings using the == operator by slicing the strings.

Example

In this example, we compares the first six characters of str1 to str2 using the == operator. It prints a message indicating whether the substring matches the given string.

package main
import (
   "fmt"
)

func main() {
   str1 := "Golang programming"
   str2 := "Golang"
   
   if str1[:6] == str2 {
      fmt.Println("The substring matches the given string.")
   } else {
      fmt.Println("The substring does not match the given string.")
   }
}

Output

It will generate the following output −

The substring matches the given string.

Comparing Strings Using Relational Operators

You can directly use relational operators (==, !=, <, >, <=, >=) to compare strings.

Example

In this example, we demonstrate how to use relational operators to compare two strings, str1 and str2. It prints the results of various comparisons, such as equality and order.

package main

import (
   "fmt"
)

func main() {
   str1 := "apple"
   str2 := "banana"
   
   fmt.Println(str1 == str2) // false
   fmt.Println(str1 != str2) // true
   fmt.Println(str1 < str2)  // true
   fmt.Println(str1 > str2)  // false
}

Output

It will generate the following output −

false
true
true
false

Case-Insensitive String Comparison

Sometimes, you may want to compare strings without considering the case. You can use the strings.EqualFold function to perform a case-insensitive comparison.

Syntax

Following is the syntax to case-insensitive string comparison in Golang −

func EqualFold(s, t string) bool

Example

In this example, we use the strings.EqualFold() function to compare two strings without considering case differences. It returns true if both strings are equal, ignoring case, and false otherwise.

package main

import (
   "fmt"
   "strings"
)

func main() {
   str1 := "GoLang"
   str2 := "golang"
   
   result := strings.EqualFold(str1, str2)
   fmt.Println(result) // true
}

Output

It will generate the following output −

true
Advertisements