stringgocase-insensitivestring-search

Case insensitive string search in golang


How do I search through a file for a word in a case insensitive manner?

For example

If I'm searching for UpdaTe in the file, if the file contains update, the search should pick it and count it as a match.


Solution

  • strings.EqualFold() can check if two strings are equal, while ignoring case. It even works with Unicode. See http://golang.org/pkg/strings/#EqualFold for more info.

    http://play.golang.org/p/KDdIi8c3Ar

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        fmt.Println(strings.EqualFold("HELLO", "hello"))
        fmt.Println(strings.EqualFold("ÑOÑO", "ñoño"))
    }
    

    Both return true.