×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to demonstrate the strings.ContainsAny() function

Here, we are going to demonstrate the strings.ContainsAny() function in Golang (Go Language).
Submitted by Nidhi, on March 18, 2021 [Last updated : March 03, 2023]

strings.ContainsAny() function in Golang

Problem Solution:

In this program, we will create some strings and then check specified string contains any given character using ContainsAny() function. The ContainsAny() function returns Boolean value to the calling function.

Program/Source Code:

The source code to demonstrate the strings.ContainsAny() function is given below. The given program is compiled and executed successfully.

Golang code to demonstrate the example of strings.ContainsAny() function

// Golang program to demonstrate the
// strings.ContainsAny() function

package main

import "fmt"
import "strings"

func main() {
	str1 := "india"
	str2 := "australia"
	str3 := "america"
	str4 := "canada"

	fmt.Println(strings.ContainsAny(str1, "iu"))
	fmt.Println(strings.ContainsAny(str2, "iu"))
	fmt.Println(strings.ContainsAny(str3, "iu"))
	fmt.Println(strings.ContainsAny(str4, "iu"))
}

Output:

true
true
true
false

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created four string variables str1, str2, str3, str4. After that, we checked specified characters in the specified string using ContainsAny() function. If anyone character is contained within the specified string then the ContainsAny() function returns true otherwise it will return false.

Golang String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.