Menu Close

Go – How to check if any characters of substring are within string ?

Here, We are going to learn about checking presence of any characters of substring are present with string in go golang. We can achieve this by using ContainsAny() function of strings package in go golang.

Function prototype:

func ContainsAny(str string, chars string) bool

Return value:

ContainsAny() function returns whether any Unicode code points in chars are within str.

Example:

package main

import (
	"fmt"
	"strings"
)

func main() {

	// 'i' does not present in 'hello'
	fmt.Println(strings.ContainsAny("hello", "i"))
	
	// 'i' in 'ui' present in 'fail'
	fmt.Println(strings.ContainsAny("fail", "ui"))
	
	// 'u' in 'ui' present in 'urge'
	fmt.Println(strings.ContainsAny("urge", "ui"))
	
	// 'u' and 'i' in 'ui' present in 'failure'
	fmt.Println(strings.ContainsAny("failure", "ui"))
	
	fmt.Println(strings.ContainsAny("foo", ""))
	
	fmt.Println(strings.ContainsAny("", ""))
}

Output:

false
true
true
true
false
false

To learn more about golang, Please refer given below link.

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/strings/
Posted in golang, packages, strings

Leave a Reply

Your email address will not be published. Required fields are marked *