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

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

Go test string contains substring

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. someString := "This is something"
  8. substring := "something"
  9. if strings.Contains(someString, substring) {
  10. fmt.Println("someString包含substring")
  11. } else {
  12. fmt.Println("someString不包含substring")
  13. }
  14. }

运行以上代码,如果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 函数。

  1. import (
  2. "strings"
  3. )
  4. strings.Contains("something", "some") // true
英文:

Use the function Contains from the strings package.

  1. import (
  2. "strings"
  3. )
  4. strings.Contains("something", "some") // true

答案2

得分: 64

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

  1. import (
  2. "fmt"
  3. "regexp"
  4. "strings"
  5. )
  6. const (
  7. str = "something"
  8. substr = "some"
  9. )
  10. // 1. Contains
  11. res := strings.Contains(str, substr)
  12. fmt.Println(res) // true
  13. // 2. Index: 检查 substr 在 str 中第一次出现的索引,如果 substr 不存在则返回 -1
  14. i := strings.Index(str, substr)
  15. fmt.Println(i) // 0
  16. // 3. Split: 通过 substr 进行分割,并检查切片的长度,如果 substr 不存在则长度为 1
  17. ss := strings.Split(str, substr)
  18. fmt.Println(len(ss)) // 2
  19. // 4. Count: 检查 str 中非重叠的 substr 实例的数量
  20. c := strings.Count(str, substr)
  21. fmt.Println(c) // 1
  22. // 5. RegExp
  23. matched, _ := regexp.MatchString(substr, str)
  24. fmt.Println(matched) // true
  25. // 6. Compiled RegExp
  26. re := regexp.MustCompile(substr)
  27. res = re.MatchString(str)
  28. fmt.Println(res) // true

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

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

To compare, there are more options:

  1. import (
  2. "fmt"
  3. "regexp"
  4. "strings"
  5. )
  6. const (
  7. str = "something"
  8. substr = "some"
  9. )
  10. // 1. Contains
  11. res := strings.Contains(str, substr)
  12. fmt.Println(res) // true
  13. // 2. Index: check the index of the first instance of substr in str, or -1 if substr is not present
  14. i := strings.Index(str, substr)
  15. fmt.Println(i) // 0
  16. // 3. Split by substr and check len of the slice, or length is 1 if substr is not present
  17. ss := strings.Split(str, substr)
  18. fmt.Println(len(ss)) // 2
  19. // 4. Check number of non-overlapping instances of substr in str
  20. c := strings.Count(str, substr)
  21. fmt.Println(c) // 1
  22. // 5. RegExp
  23. matched, _ := regexp.MatchString(substr, str)
  24. fmt.Println(matched) // true
  25. // 6. Compiled RegExp
  26. re = regexp.MustCompile(substr)
  27. res = re.MatchString(str)
  28. 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).

  1. BenchmarkStringsContains-4 100000000 10.5 ns/op 0 B/op 0 allocs/op
  2. BenchmarkStringsIndex-4 117090943 10.1 ns/op 0 B/op 0 allocs/op
  3. BenchmarkStringsSplit-4 6958126 152 ns/op 32 B/op 1 allocs/op
  4. BenchmarkStringsCount-4 42397729 29.1 ns/op 0 B/op 0 allocs/op
  5. BenchmarkStringsRegExp-4 461696 2467 ns/op 1326 B/op 16 allocs/op
  6. 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:

确定