AppDividend
Latest Code Tutorials

Golang String EqualFold() Function Example

0

Golang String EqualFold() is an inbuilt function that reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity. Uppercase letters are not equal to lowercase letters. But with EqualFold() function, we fold the cases.

This makes “A” equal to “a.” No string copies or conversions are required.

Golang String EqualFold()

In Golang, we can use the equality operator to test the contents of strings. This treats uppercase and lowercase characters as different. Golang strings.EqualFold() can be used for case insensitivity. That means if the rune is the same, but the cases are different, then still it returns true.

Syntax

func EqualFold(s, t string) bool

Parameters

The function takes two arguments, and both are strings. We check against each other, and if the runes are the same, then regardless of the cases, it will return true.

Example

// hello.go

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.EqualFold("QUIBI", "quibi"))
}

Output

go run hello.go
true

In the above code, both strings are the same, but the cases are different. One is uppercase, and one is lowercase.

Let’s see another example.

// hello.go

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.EqualFold("peacock", "quibi"))
}

Output

go run hello.go
false

Conclusion

With the equals-sign operator and strings.EqualFold(), we can compare strings. Case-insensitivity is not always needed. Using the equals operator is best when it is sufficient.

See also

How to compare two Strings

Golang String Contains()

Golang String make()

Golang String ContainsAny()

Golang String Index()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.