Syntax error What is the EqualFold function in Golang?

What is the EqualFold function in Golang?



The EqualFold() function in Golang is an inbuilt function of strings package which is used to check whether the given strings (UTF-8 strings) are equal. The comparison is not case-sensitive. It accepts two string parameters and returns True if both the strings are equal under Unicode case-folding (i.e., case-insensitive), False otherwise.

Syntax

func EqualFold(s, t string) bool

Where s and t are strings. It returns a Boolean value.

Example

The following example demonstrates how to use EqualFold() in a Go program −

package main
import (
   "fmt"
   "strings"
)
func main() {

   // Intializing the Strings
   R := "Welcome to Tutorialspoint"
   S := "WELCOME TO TUTORIALSPOINT"
   fmt.Println("First string:", R)
   fmt.Println("Second string:", S)

   // using the EqualFold function
   if strings.EqualFold(R, S) == true {
      fmt.Println("Both the strings are equal")
   } else {
      fmt.Println("The strings are not equal.")
   }
}

Output

It will generate the following output −

First string: Welcome to Tutorialspoint
Second string: WELCOME TO TUTORIALSPOINT
Both the strings are equal

Observe that the comparison is not case-sensitive.

Example

Let us take another example −

package main
import (
   "fmt"
   "strings"
)
func main() {

   // Initializing the Strings
   x := "Prakash"
   y := "PRAKASH"
   z := "Vijay"
   w := "Pruthvi"

   // Display the Strings
   fmt.Println("First string:", x)
   fmt.Println("Second string:", y)
   fmt.Println("Third string:", z)
   fmt.Println("Fourth string:", w)

   // Using the EqualFold Function
   test1 := strings.EqualFold(x, y)
   test2 := strings.EqualFold(z, w)
   
   // Display the EqualFold Output
   fmt.Println("First Two Strings are Equal? :", test1)
   fmt.Println("Last Two Strings are Equal? :", test2)
}

Output

It will produce the following output −

First string: Prakash
Second string: PRAKASH
Third string: Vijay
Fourth string: Pruthvi
First Two Strings are Equal? : true
Last Two Strings are Equal? : false
Updated on: 2022-03-10T07:00:55+05:30

691 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements