In Go, I want to check if the following string contains text/plain
in a string. The function strings.Contains() returns always false
.
My local go version is go1.14.3 windows/amd64, my server version is go1.13.3 linux/amd64
Test code (play.golang.org/p/_ikCzWd6438)
var test = "text/plain; charset=utf-8"
fmt.Println("my string:", test)
fmt.Println("strings.Contains(text/plain)", strings.Contains("text/plain", test))
fmt.Println("strings.Contains(text)", strings.Contains("text", test))
fmt.Println("strings.Contains(charset)", strings.Contains("charset", test))
var test2 = strings.ReplaceAll(test, "/", "") // remove slash
fmt.Println("my second string:", test2)
fmt.Println("strings.Contains(textplain)", strings.Contains("textplain", test2))
fmt.Println("strings.Contains(text)", strings.Contains("text", test2))
fmt.Println("strings.Contains(charset)", strings.Contains("charset", test2))
Output
my string: text/plain; charset=utf-8
strings.Contains(text/plain) false
strings.Contains(text) false
strings.Contains(charset) false
my second string: textplain; charset=utf-8
strings.Contains(textplain) false
strings.Contains(text) false
strings.Contains(charset) false
So why returns strings.Contains()
always 'false'?
According to the docs:
func Contains(s, substr string) bool
Contains reports whether substr is within s.
The first parameter s
is the original string and the second, substr
, is the substring you search for. In your case, it is the other way around.
For example instead of strings.Contains("charset", test)
, it should be strings.Contains(test, "charset")