×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang strings.EqualFold() Function with Examples

Golang | strings.EqualFold() Function: Here, we are going to learn about the EqualFold() function of strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 15, 2021

strings.EqualFold()

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

Syntax

func EqualFold(st1, str2 string) bool

Parameters

  • str1 : First string.
  • str2 : Second string.

Return Value

The return type of EqualFold() function is a bool, it returns true if both strings are equal under Unicode case-folding (i.e., case-insensitive), false otherwise.

Example 1

// Golang program to demonstrate the
// example of strings.EqualFold() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.EqualFold("Hello", "hello"))
	fmt.Println(strings.EqualFold("HELLO, WORLD!", "Hello, world!"))
	fmt.Println(strings.EqualFold("Hello There?", "Hi There?"))
}

Output:

true
true
false

Example 2

// Golang program to demonstrate the
// example of strings.EqualFold() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str1 string
	var str2 string

	str1 = "Hello"
	str2 = "hello"

	if strings.EqualFold(str1, str2) == true {
		fmt.Printf("\"%s\" and \"%s\" are equal under Unicode case-folding\n", str1, str2)
	} else {
		fmt.Printf("\"%s\" and \"%s\" are not equal under Unicode case-folding\n", str1, str2)
	}

	str1 = "HELLO, WORLD!"
	str2 = "Hello, world!"

	if strings.EqualFold(str1, str2) == true {
		fmt.Printf("\"%s\" and \"%s\" are equal under Unicode case-folding\n", str1, str2)
	} else {
		fmt.Printf("\"%s\" and \"%s\" are not equal under Unicode case-folding\n", str1, str2)
	}

	str1 = "Hello There?"
	str2 = "Hi There?"

	if strings.EqualFold(str1, str2) == true {
		fmt.Printf("\"%s\" and \"%s\" are equal under Unicode case-folding\n", str1, str2)
	} else {
		fmt.Printf("\"%s\" and \"%s\" are not equal under Unicode case-folding\n", str1, str2)
	}
}

Output:

"Hello" and "hello" are equal under Unicode case-folding
"HELLO, WORLD!" and "Hello, world!" are equal under Unicode case-folding
"Hello There?" and "Hi There?" are not equal under Unicode case-folding

Golang strings Package Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.