Go语言有大小写不敏感的字符串contains()函数吗?

xxhby3vn  于 2023-02-17  发布在  Go
关注(0)|答案(4)|浏览(267)

我希望能够判断stringB是否是stringA的不区分大小写的子字符串,在Go语言的strings pkg中,我能找到的最接近的是strings.Contains(strings.ToLower(stringA), strings.ToLower(stringB),是否有一个更简洁的替代方案?

km0tfn4u

km0tfn4u1#

如果你只是不喜欢冗长的代码,你可以尝试让你的代码格式更简洁,例如:

strings.Contains(
    strings.ToLower(stringA),
    strings.ToLower(stringB),
)

或者将其隐藏在您自己的utils(或其他)包的函数中:

package utils

import "strings"

func ContainsI(a string, b string) bool {
    return strings.Contains(
        strings.ToLower(a),
        strings.ToLower(b),
    )
}
4dc9hkyq

4dc9hkyq2#

另一种选择:

package main
import "regexp"

func main() {
   b := regexp.MustCompile("(?i)we").MatchString("West East")
   println(b)
}

https://golang.org/pkg/regexp/syntax

czq61nw1

czq61nw13#

我在标准套餐里没有看到。这个怎么样?

package main

import (
    "fmt"
    "strings"
)

func strcasestr(a, b string) bool {
    d := len(a)
    if d == 0 {
        return true
    }
    xx := strings.ToLower(a[0:1]) + strings.ToUpper(a[0:1])
    for i := 0; i <= len(b)-len(a); i++ {
        i = strings.IndexAny(b, xx)
        if i == -1 || i+d > len(b) {
            break
        }
        if d == 1 {
            return true
        }
        if strings.EqualFold(a[1:], b[i+1:i+d]) {
            return true
        }
    }
    return false
}

func main() {
    examples := []struct {
        a, b string
    }{
        {"APP", "apple pie"},
        {"Read", "banana bread"},
        {"ISP", "cherry crisp"},
        {"ago", "dragonfruit tart"},
        {"INC", "elderberry wine"},
        {"M", "Feijoa jam"},
    }
    for i, e := range examples {
        fmt.Println(i, ":", e.a, " in ", e.b, "? ", strcasestr(e.a, e.b))
    }
}
qacovj5a

qacovj5a4#

通过比较strings.Contains(strings.ToLower(s), strings.ToLower(substr))regexp.MustCompile("(?i)" + regexp.QuoteMeta(substr)).MatchString(s)的使用情况的基准测试来扩展Zombo的答案。

代码

import (
    "regexp"
    "strings"
    "testing"
)

const checkStringLen38 = "Hello RiCHard McCliNTock. How are you?"
const checkStringLen3091 = `What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

Where does it come from?

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. RiCHard McCliNTock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
Where can I get some?

There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.`
const searchQuery = "richard mcclintock"

func BenchmarkContainsLowerLowerShort(b *testing.B) {
    for n := 0; n < b.N; n++ {
        strings.Contains(strings.ToLower(checkStringLen38), strings.ToLower(searchQuery))
    }
}

func BenchmarkContainsLowerLowerLong(b *testing.B) {
    for n := 0; n < b.N; n++ {
        strings.Contains(strings.ToLower(checkStringLen3091), strings.ToLower(searchQuery))
    }
}

func BenchmarkRegexpShort(b *testing.B) {
    for n := 0; n < b.N; n++ {
        regexp.MustCompile("(?i)" + regexp.QuoteMeta(searchQuery)).MatchString(checkStringLen38)
    }
}

func BenchmarkRegexpLong(b *testing.B) {
    for n := 0; n < b.N; n++ {
        regexp.MustCompile("(?i)" + regexp.QuoteMeta(searchQuery)).MatchString(checkStringLen3091)
    }
}

func BenchmarkRegexpShortPrebuilt(b *testing.B) {
    prebuiltRegExp := regexp.MustCompile("(?i)" + regexp.QuoteMeta(searchQuery))
    for n := 0; n < b.N; n++ {
        prebuiltRegExp.MatchString(checkStringLen38)
    }
}

func BenchmarkRegexpLongPrebuilt(b *testing.B) {
    prebuiltRegExp := regexp.MustCompile("(?i)" + regexp.QuoteMeta(searchQuery))
    for n := 0; n < b.N; n++ {
        prebuiltRegExp.MatchString(checkStringLen3091)
    }
}

结果编号

>go test -bench=. ./...
goos: windows
goarch: amd64
cpu: Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
BenchmarkContainsLowerLowerShort-8       9147040               130.3 ns/op
BenchmarkContainsLowerLowerLong-8         158318              7594 ns/op
BenchmarkRegexpShort-8                    364604              3262 ns/op
BenchmarkRegexpLong-8                      40394             29851 ns/op
BenchmarkRegexpShortPrebuilt-8           3741936               328.8 ns/op
BenchmarkRegexpLongPrebuilt-8              44394             27264 ns/op

结果解释

当只搜索短字符串时,使用regexp.MustCompile("(?i)" + regexp.QuoteMeta(substr)).MatchString(s)会有很大的好处(一个数量级)从建立*regexp.Regexp只一次。然而,即使这样,对于长输入字符串和短输入字符串,执行strings.Contains(strings.ToLower(s), strings.ToLower(substr))也要花费大约三倍的时间(我们甚至没有检查如果我们假设查询字符串已经是小写的,ToLower()-variant会快多少)即使如此,也只是在substr总是相同的约束下的情况,因为否则仅构建正则表达式一次不是选项。

TL;医生

strings.Contains(strings.ToLower(s), strings.ToLower(substr))上使用regexp.MustCompile("(?i)" + regexp.QuoteMeta(substr)).MatchString(s)没有任何好处

相关问题