Case insensitive string search in golang

后端 未结 4 1104
清歌不尽
清歌不尽 2020-12-24 13:09

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

For example

If I\'m searching for UpdaTe in

4条回答
  •  情歌与酒
    2020-12-24 13:55

    Do not use strings.Contains unless you need exact matching rather than language-correct string searches

    None of the current answers are correct unless you are only searching ASCII characters the minority of languages (like english) without certain diaeresis / umlauts or other unicode glyph modifiers (the more "correct" way to define it as mentioned by @snap). The standard google phrase is "searching non-ASCII characters".

    For proper support for language searching you need to use http://golang.org/x/text/search.

    func SearchForString(str string, substr string) (int, int) {
        m := search.New(language.English, search.IgnoreCase)
        return = m.IndexString(str, substr)
    }
    
    start, end := SearchForString('foobar', 'bar');
    if start != -1 && end != -1 {
        fmt.Println("found at", start, end);
    }
    

    Or if you just want the starting index:

    func SearchForStringIndex(str string, substr string) (int, bool) {
        m := search.New(language.English, search.IgnoreCase)
        start, _ := m.IndexString(str, substr)
        if start == -1 {
            return 0, false
        }
        return start, true
    }
    
    index, found := SearchForStringIndex('foobar', 'bar');
    if found {
        fmt.Println("match starts at", index);
    }
    

    Search the language.Tag structs here to find the language you wish to search with or use language.Und if you are not sure.

    Update

    There seems to be some confusion so this following example should help clarify things.

    package main
    
    import (
        "fmt"
        "strings"
    
        "golang.org/x/text/language"
        "golang.org/x/text/search"
    )
    
    var s = `Æ`
    var s2 = `Ä`
    
    func main() {
        m := search.New(language.Finnish, search.IgnoreDiacritics)
        fmt.Println(m.IndexString(s, s2))
        fmt.Println(CaseInsensitiveContains(s, s2))
    }
    
    // CaseInsensitiveContains in string
    func CaseInsensitiveContains(s, substr string) bool {
        s, substr = strings.ToUpper(s), strings.ToUpper(substr)
        return strings.Contains(s, substr)
    }
    

提交回复
热议问题