Menu Close

Go – Compare two byte slices case insensitive in go

Here, bytes.EqualFold() function is used to compare two byte slices case insensitive in go.

EqualFold() built-in function of bytes package compare two byte slices which are interpreted as UTF-8 strings, are equal under simple Unicode case-folding which is a more general form of case-insensitivity.

bytes.EqualFold() function prototype:

func EqualFold(s, t []byte) bool

Input parameters:
s: slice of bytes
t: slice of bytes

Return:
true if both are equal in case insensitive else false.

Explanation on compare two byte slices case insensitive in go:

1)
s := []byte("techieindoor")
t:= []byte("TechieIndoor")

Output:
true

2)
s := []byte("techieindoor")
sep := []byte("TECHIEINDOOR")

Output:
true

3)
s := []byte("techieindoor")
sep := []byte("Techie")

Output:
false

Example:

Code 1:

package main

import (
    "fmt"
    "bytes"
)


func main() {
    s := []byte("techieindoor")
    t := []byte("TechieIndoor")
    
    fmt.Println(bytes.EqualFold(s, t))
    
    s = []byte("techieindoor")
    t = []byte("TECHIEINDOOR")
    
    fmt.Println(bytes.EqualFold(s, t))
    
    
    s = []byte("techieindoor")
    t = []byte("Techie")
    
    fmt.Println(bytes.EqualFold(s, t))
}

Output:

% go run sample.go

true
true
false

To learn more about golang, Please refer given below link:

References:

https://golang.org/doc/

Posted in bytes, golang, packages

Leave a Reply

Your email address will not be published.

Contact Us