Go测试字符串是否包含子字符串

huangapple go评论73阅读模式
英文:

Go test string contains substring

问题

在Go语言中,你可以使用strings包中的Contains函数来检查一个字符串是否是另一个字符串的子串。例如,你可以使用以下代码来检查someString是否包含"something":

package main

import (
	"fmt"
	"strings"
)

func main() {
	someString := "This is something"
	substring := "something"

	if strings.Contains(someString, substring) {
		fmt.Println("someString包含substring")
	} else {
		fmt.Println("someString不包含substring")
	}
}

运行以上代码,如果someString包含"something",则会输出"someString包含substring";否则,会输出"someString不包含substring"。

英文:

How do I check if a string is a substring of another string in Go?
For example, I want to check someString.contains("something").

答案1

得分: 372

使用 strings 包中的 Contains 函数。

import (
    "strings"
)
strings.Contains("something", "some") // true
英文:

Use the function Contains from the strings package.

import (
    "strings"
)
strings.Contains("something", "some") // true

答案2

得分: 64

要进行比较,有更多的选项:

import (
	"fmt"
	"regexp"
	"strings"
)

const (
	str    = "something"
	substr = "some"
)

// 1. Contains
res := strings.Contains(str, substr)
fmt.Println(res) // true

// 2. Index: 检查 substr 在 str 中第一次出现的索引,如果 substr 不存在则返回 -1
i := strings.Index(str, substr)
fmt.Println(i) // 0

// 3. Split: 通过 substr 进行分割,并检查切片的长度,如果 substr 不存在则长度为 1
ss := strings.Split(str, substr)
fmt.Println(len(ss)) // 2

// 4. Count: 检查 str 中非重叠的 substr 实例的数量
c := strings.Count(str, substr)
fmt.Println(c) // 1

// 5. RegExp
matched, _ := regexp.MatchString(substr, str)
fmt.Println(matched) // true

// 6. Compiled RegExp
re := regexp.MustCompile(substr)
res = re.MatchString(str)
fmt.Println(res) // true

基准测试:
Contains 内部调用了 Index,因此速度几乎相同(顺便说一下,Go 1.11.5 显示的差异比 Go 1.14.3 大一些)。

BenchmarkStringsContains-4              100000000               10.5 ns/op             0 B/op          0 allocs/op
BenchmarkStringsIndex-4                 117090943               10.1 ns/op             0 B/op          0 allocs/op
BenchmarkStringsSplit-4                  6958126               152 ns/op              32 B/op          1 allocs/op
BenchmarkStringsCount-4                 42397729                29.1 ns/op             0 B/op          0 allocs/op
BenchmarkStringsRegExp-4                  461696              2467 ns/op            1326 B/op         16 allocs/op
BenchmarkStringsRegExpCompiled-4         7109509               168 ns/op               0 B/op          0 allocs/op
英文:

To compare, there are more options:

import (
	"fmt"
	"regexp"
	"strings"
)

const (
    str    = "something"
	substr = "some"
)

// 1. Contains
res := strings.Contains(str, substr)
fmt.Println(res) // true

// 2. Index: check the index of the first instance of substr in str, or -1 if substr is not present
i := strings.Index(str, substr)
fmt.Println(i) // 0

// 3. Split by substr and check len of the slice, or length is 1 if substr is not present
ss := strings.Split(str, substr)
fmt.Println(len(ss)) // 2

// 4. Check number of non-overlapping instances of substr in str
c := strings.Count(str, substr)
fmt.Println(c) // 1

// 5. RegExp
matched, _ := regexp.MatchString(substr, str)
fmt.Println(matched) // true

// 6. Compiled RegExp
re = regexp.MustCompile(substr)
res = re.MatchString(str)
fmt.Println(res) // true

Benchmarks:
Contains internally calls Index, so the speed is almost the same (btw Go 1.11.5 showed a bit bigger difference than on Go 1.14.3).

BenchmarkStringsContains-4              100000000               10.5 ns/op             0 B/op          0 allocs/op
BenchmarkStringsIndex-4                 117090943               10.1 ns/op             0 B/op          0 allocs/op
BenchmarkStringsSplit-4                  6958126               152 ns/op              32 B/op          1 allocs/op
BenchmarkStringsCount-4                 42397729                29.1 ns/op             0 B/op          0 allocs/op
BenchmarkStringsRegExp-4                  461696              2467 ns/op            1326 B/op         16 allocs/op
BenchmarkStringsRegExpCompiled-4         7109509               168 ns/op               0 B/op          0 allocs/op

huangapple
  • 本文由 发表于 2017年7月23日 23:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/45266784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定