I would like to be able to determine whether stringB is a case-insensitive substring of stringA. Looking through Go's strings
pkg, the closest I can get is strings.Contains(strings.ToLower(stringA), strings.ToLower(stringB)
. Is there a less wordy alternative that I'm not seeing?

- 7,243
- 6
- 49
- 61
-
1With strings - no, but perhaps using stringB as a regex pattern? – Not_a_Golfer Jun 16 '17 at 18:22
-
2No, but as always if you need to type it more than once you can put it in your own function. – JimB Jun 16 '17 at 18:31
-
Possible duplicate of [Case insensitive string search in golang](https://stackoverflow.com/questions/24836044/case-insensitive-string-search-in-golang) – Oleg Kovalov Jun 16 '17 at 18:32
-
I would worry more about performance than elegance of the code, if you look at `strings.EqualsFold` you'll see that it doesn't just fold the strings and compare, which will allocate memory, it tries to do a case insensitive comparison in place. – Not_a_Golfer Jun 16 '17 at 20:04
-
1@Not_a_Golfer `string.EqualsFold` tests for equality, I need to test for substring – Jun 16 '17 at 20:32
-
@sreya I know, I just meant that it avoids copying strings and allocating memory, and what you are proposing does not. – Not_a_Golfer Jun 17 '17 at 20:14
4 Answers
If it's just the wordiness that you dislike, you could try making your code formatting cleaner, e.g.:
strings.Contains(
strings.ToLower(stringA),
strings.ToLower(stringB),
)
Or hiding it in a function in your own utils
(or whatever) package:
package utils
import "strings"
func ContainsI(a string, b string) bool {
return strings.Contains(
strings.ToLower(a),
strings.ToLower(b),
)
}

- 7,243
- 6
- 49
- 61

- 407
- 3
- 7
-
2Great, but this is exactly what this article explains as not very performant, and he is right, creating a copy each time is not a good idea. https://www.digitalocean.com/community/questions/how-to-efficiently-compare-strings-in-go – Melardev Jul 23 '20 at 09:36
Another option:
package main
import "regexp"
func main() {
b := regexp.MustCompile("(?i)we").MatchString("West East")
println(b)
}

- 1
- 62
- 391
- 407
I don't see one in the standard packages. How about this?
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))
}
}

- 865
- 7
- 9
Expanding on Zombo's answer with a benchmark comparing the use of strings.Contains(strings.ToLower(s), strings.ToLower(substr))
to the use of regexp.MustCompile("(?i)" + regexp.QuoteMeta(substr)).MatchString(s)
.
Code
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)
}
}
Results
>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
Interpretation of Results
When only searching short strings, use of regexp.MustCompile("(?i)" + regexp.QuoteMeta(substr)).MatchString(s)
benefits greatly (one order of magnitude) from building *regexp.Regexp
only once. However, even then it takes about three times as long to execute as strings.Contains(strings.ToLower(s), strings.ToLower(substr))
for both long as well as short input strings (and we did not even check how much faster the ToLower()
-variant would be if we assumed that the query string already was lower-cased) and even that is only the case under the constraint that substr
is always the same, because building the regular expression only once is not an option otherwise.
tl;dr
There is nothing to be gained by using regexp.MustCompile("(?i)" + regexp.QuoteMeta(substr)).MatchString(s)
over strings.Contains(strings.ToLower(s), strings.ToLower(substr))
.

- 2,690
- 2
- 22
- 27