How to check String for equality under Unicode case folding in Golang?

In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
In Go strings, you are allowed to compare two strings with each other without any error even if they are written in different cases(lowercase and uppercase) with the help of EqualFold() function. Or in other, this function is used to check whether the specified strings are interpreted as UTF-8 strings and are equal under Unicode case-folding. This function returns true if the given strings are equal under Unicode case-folding or return false if the given strings are not equal under Unicode case-folding. It is defined under the strings package so, you have to import strings package in your program for accessing EqualFold function.

Syntax:

func EqualFold(str1, str2 string) bool

The return type of this function is of the bool type. Let us discuss this concept with the help of the examples:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to
// check the given strings are equal or not
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating strings
    // Using var keyword
    var username string
    var uinput string
  
    // Initializing strings
    username = "AnkitaSaini"
    uinput = "ankitasaINI"
  
    // Checking whether the given
    // strings are equal or not
    // Using EqualFold() function
    res := strings.EqualFold(username, uinput)
  
    // Displaying the result according
    // to the given condition
    if res == true {
  
        fmt.Println("!..Successfully Matched..!")
    } else {
  
        fmt.Println("!..Not Matched..!")
    }
  
}

chevron_right


Output:

!..Successfully Matched..!

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check
// the given strings are equal or not
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing strings
    // Using shorthand declaration
    s1 := "I am working as a Technical content writer, in GeeksforGeeks!"
    s2 := "I am currently writing articles on Go language!"
  
    // Checking the given strings are equal or not
    // Using EqualFold() function
    res1 := strings.EqualFold(s1, "I AM WORKING AS A TECHNICAL CONTENT WRITER, IN GEEKSFORGEEKS!")
    res2 := strings.EqualFold(s1, "I AM working AS A Technical CONTENT writer, IN GeeksforGeeks!")
    res3 := strings.EqualFold(s1, "Apple")
    res4 := strings.EqualFold(s2, "I am currently writing articles on Go language!")
    res5 := strings.EqualFold(s2, "I am currently writing ARTICLES on Go language!")
    res6 := strings.EqualFold("GeeksforGeeks", "geeksForgeeks")
  
    // Displaying results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
    fmt.Println("Result 4: ", res4)
    fmt.Println("Result 5: ", res5)
    fmt.Println("Result 6: ", res6)
  
}

chevron_right


Output:

Result 1:  true
Result 2:  true
Result 3:  false
Result 4:  true
Result 5:  true
Result 6:  true


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.