ContainsAny...Understanding some false statements

In strings.ContainsAny, why are
fmt.Println(strings.ContainsAny(“foo”, “”))
fmt.Println(strings.ContainsAny("", “”))
false.

First example: there are neither f nor o in the empty string. So the returned value is false.

Second example: as there are no letters in the left string, there can’t be any char of them in the second string, so the returned value is false.

Generally: the empty string can’t contain any character from the input string by the definition as it is empty.

func Contains(s, substr string) bool

Contains reports whether substr is within s.

The condition is that the second element be in the first, isn’t it? So the question is whether
“” is contained in “foo”, isn’t it? Which to me, it appears that it is.

I saw a moment ago that “”, “” are different types of quotation marks, so maybe that’s what’s going on there?

I have to be honest, I just realised, that I got the arguments in my first post wrong.

Though the core doesn’t change: there is no single rune in "" that could be found in any other string, so ContainsAny() will be false for any call that has an empty chars argument.

Contains will indeed check for substrings, and if the substr is the empty string, then I’d expect it to be found in any string as well.

So the quotation marks (and I discovered that all of them are smart quotes in The Go Programming Language) so the quotation marks themselves are not runes?

Interesting that
fmt.Println(strings.Contains(“seafood”, “”))
is true, whereas
fmt.Println(strings.ContainsAny(“foo”, “”))
is false.

All the quotation marks should be smart ones. I may have to reset that on my computer again.

The quotation marks are to begin and end the string. The runes are choosen from the string. So if you want to search for an actual quotation mark you have to provide a string that contains it. "\"".

Oh OK

So why is this true? What’s the difference?

Contains checks for the existence of a substring, not for the existence of an item from a given set. And the empty string is contained in any other string.

1 Like

Interesting. I think I’ve got it now.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.