GoLang String - Compare()

In this tutorial, we will learn about string comparison and how it can be done lexicographically,in Go Compare() method is used to compare the two strings.

let us explore the uses case with help of simple examples.

String Compare:

It returns 0 when both the strings are equal, if they are not equal: Two cases will arise.

  1. If string1 is greater than string2, then 1 is returned.
  2. If string1 is less than string2, then -1 is returned.

Syntax:


strings.Compare(first_string,second_string)

Where, first_string is string1 and second_string is string2.

String compares examples:

It is important to specify the "strings" package in import.

Example 1:

Let us consider the two strings - "Hello" and "Gkindex" and compare both.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider two strings
    var first = "Hello"
    var second = "Gkindex"
 
    fmt.Println("String 1: ", first)
    fmt.Println("String 2: ", second)
    
    // Compare two strings.
      fmt.Println("Comparison: ", strings.Compare(first,second))
}

Output:

CopiedCopy Code

String 1:  Hello
String 2:  Gkindex
Comparison:  1

Explanation for the above output:

Here the first string starts with 'H' and second string starts with 'G'.

ASCII Value of 'H'(72) is greater than ASCII Value of 'G' (71).

So, first string is greater than the second string. Hence 1 is returned.

Example 2:

Let us consider the two strings - "Hello" and "hello" and compare both of them.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider two strings
    var first = "Hello"
    var second = "hello"
 
    fmt.Println("String 1: ", first)
    fmt.Println("String 2: ", second)
    
    // Compare two strings.
      fmt.Println("Comparison: ", strings.Compare(first,second))
}

Output:

CopiedCopy Code

String 1:  Hello
String 2:  hello
Comparison:  -1

Explanation for the above output:

Explanation for the above output:

Here the first string starts with 'H' and second string starts with 'h'.

ASCII Value of 'H'(72) is less than ASCII Value of 'h' (104).

So, first string is less than second string. Hence -1 is returned.

Example 3:

Let us consider the two strings - "Hello" and "Hello" and compare both of them.

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider two strings
    var first = "Hello"
    var second = "Hello"
 
    fmt.Println("String 1: ", first)
    fmt.Println("String 2: ", second)
    
    // Compare two strings.
      fmt.Println("Comparison: ", strings.Compare(first,second))
}

Output:

CopiedCopy Code

String 1:  Hello
String 2:  Hello
Comparison:  0

Explanation for the above output:

Here the first string starts with 'H' and second string starts with 'H'.

ASCII Value of 'H'(72) is equal ASCII Value of 'H' (72).

So, first string is equal to the second string. Hence 0 is returned.

Conclusion

Now we know how to compare strings using the Compare() function in a Golang and understood the implementation with working examples.