Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Case insensitive string comparison in Go (Golang)

Posted on March 25, 2023March 25, 2023 by admin

Table of Contents

  • Overview
  • Code:

Overview

In Golang string are UTF-8 encoded. strings package of GO provides an EqualFold method that can be used to do case insensitive comparison of two strings in Go.

Below is the signature of the function. The methods return boolean indicating whether the two strings supplied are case insensitive equal or not.

func EqualFold(s, t string) bool

Code:

package main
import (
"fmt"
"strings"
)
func main() {
res := strings.EqualFold("abc", "ABC")
fmt.Println(res)
res = strings.EqualFold("abc", "aBC")
fmt.Println(res)
res = strings.EqualFold("abc", "AbC")
fmt.Println(res)
res = strings.EqualFold("abc", "AbCd")
fmt.Println(res)
}

Output:

true
true
true
false
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP