Golang Program to compare two strings by ignoring case


In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. In this article we will write a go language program to compare two strings by ignoring their case. Here, we will use both for loops and inbuilt library functions defined in go language to implement the result.

Syntax

func ToLower(r rune) rune

ToLower() function present in unicode package is used to convert a given string to lowercase. It takes the rune character as an argument and returns a rune in lowercase by converting the given rune to lowercase. This function will be used in both the methods in this article.

Method 1: Using internal function

In this method, we are going to learn how to create a user-defined function that will help in comparing 2 strings regardless of their case.

Algorithm

  • Step 1 − First, we need to import the fmt and strings package.

  • Step 2 − Then, start the main() function. Inside the main() initialize two variables of string data type and store values to them.

  • Step 3 − Further print both the strings on the screen by using fmt.Println() function. If the length of both the strings are different then print that the strings have different lengths and stop the further execution of program.

  • Step 4 − Then, use a for loop to iterate over the first string and use ToLower() function to change each character of string to lowercase. Meanwhile compare each character with the help of if condition.

  • Step 5 − If any character of the string turns out to be different then print that the strings are not equal and stop further execution of the program.

  • Step 6 − If the for loop completes then it means that the two strings were equal with case being the only difference. So, print both strings are equal ignoring the case.

Example

In this example, we will write a go language program to compare two strings by ignoring their case with the help of for loop

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str1 string = "Hello World"
   var str2 string = "HELLO WORLD"
   fmt.Println("The first string is:\n", str1)
   fmt.Println("The second string is:\n", str2)
   fmt.Println()
   if len(str1) != len(str2) {
      fmt.Println("The strings have different length.")
      return
   }

   for i := 0; i < len(str1); i++ {
      if strings.ToLower(string(str1[i])) != strings.ToLower(string(str2[i])) {
         fmt.Println("The strings are not equal.")
         return
      }
   }

   fmt.Println("The two given strings are equal, ignoring case.")
}

Output

The first string is:
 Hello World
The second string is:
 HELLO WORLD

The two given strings are equal, ignoring case.

Method 2: Using Internal function of Golang

In this method, we are going to learn about 2 internal function of Golang – Compare() and contains(). Both of these syntax are explained below −

Syntax

func Compare(a, b string) int

The compare() function is present in strings package and is used to compare two string values. This function takes the two strings as argument and returns an integer value based on whether the first string is greater, lesser or equal to the second string.

func Contains(str, substr string) bool

The contain() function is present in strings package and is used to check whether a given substring is present in the string or not. The function accepts the two strings as arguments to the function and returns a Boolean value based on whether the substring is found or not. If the value returned by the function is true then the substring is found and vice-versa.

func HasPrefix(str, prefix string) bool

HasPrefix() function is present in strings package and it is used to check whether a substring is a part of given string or not. The function accepts two arguments one is the string to be checked and other is the substring the function then returns a Boolean variable depending upon whether the string starts with a particular substring or not.

Algorithm

  • Step 1 − First, we need to import the fmt and strings package.

  • Step 2 − Then, start the main() function. Inside the main() initialize two variables of string data type and store elements in it. further, print both the strings on the screen.

  • Step 3 − Now, use ToLower() function to convert both the strings in lowercase and compare them with the help of Compare() function present in strings package.

  • Step 4 − If the value returned by the function turns out to be zero then it means that both the strings are equal so print it on the screen.

  • Step 5 − Otherwise print that the strings are not equal.

Example 1

In this example we will write a go language program to compare two strings by ignoring the case with the help of compare() function.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str1 string = "Hello World"
   var str2 string = "HELLO WORLD"
   fmt.Println("The first string is:\n", str1)
   fmt.Println("The second string is:\n", str2)
   fmt.Println()

   if strings.Compare(strings.ToLower(str1), strings.ToLower(str2)) == 0 {
      fmt.Println("The strings are equal, ignoring case.")
   } else {
      fmt.Println("The strings are not equal.")
   }
}

Output

The first string is:
 Hello World
The second string is:
 HELLO WORLD

The strings are equal, ignoring case.

Example 2

In this example we will write a go language program to compare two strings with the help of contains() function.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str1 string = "Hello World"
   var str2 string = "hI"
   fmt.Println("The first string is:\n", str1)
   fmt.Println("The second string is:\n", str2)
   fmt.Println()

   if strings.Contains(strings.ToLower(str1), strings.ToLower(str2)) {
      fmt.Println("The first string contains the second string, ignoring case.")
   } else {
      fmt.Println("The first string does not contain the second string.")
   }
}

Output

The first string is:
 Hello World
The second string is:
 hI

The first string does not contain the second string.

Example 3

In this example we will write a go language program to compare two strings with the help of HasPrefix() function.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str1 string = "Hello World"
   var str2 string = "hELLo"
   fmt.Println("The first string is:\n", str1)
   fmt.Println("The second string is:\n", str2)
   fmt.Println()

   if strings.HasPrefix(strings.ToLower(str1), strings.ToLower(str2)) {
      fmt.Println("The first string starts with the second string, ignoring case.")
   } else {
      fmt.Println("The first string does not start with the second string.")
   }
}

Output

The first string is:
 Hello World
The second string is:
 hELLo

The first string starts with the second string, ignoring case.

Conclusion

We have successfully compiled and executed a Golang Program to compare two strings by ignoring case with the help of examples.

Updated on: 17-Feb-2023

827 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements