In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to check the given characters present in the string using the given functions. These functions are defined under strings package, so you have to import strings package in your program to access these functions:
1. Contains: This function is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false.
Syntax:
func Contains(str, chstr string) bool
Here, str is the original string and chstr is the string which you wants to check. Let us discuss this concept with the help of an example:
Example:
// Go program to illustrate how to check // the string is present or not in the // specified string package main import ( "fmt" "strings") // Main function func main() { // Creating and initializing strings str1 := "Welcome to Geeks for Geeks" str2 := "Here! we learn about go strings" fmt.Println("Original strings") fmt.Println("String 1: ", str1) fmt.Println("String 2: ", str2) // Checking the string present or not // Using Contains() function res1 := strings.Contains(str1, "Geeks") res2 := strings.Contains(str2, "GFG") // Displaying the result fmt.Println("\nResult 1: ", res1) fmt.Println("Result 2: ", res2) } |
Output:
Original strings String 1: Welcome to Geeks for Geeks String 2: Here! we learn about go strings Result 1: true Result 2: false
2. ContainsAny: This function is used to check whether any Unicode code points in chars are present in the given string. If any Unicode code points in chars are available in the given string, then this method returns true, otherwise, return false.
Syntax:
func ContainsAny(str, charstr string) bool
Here, str is the original string and charstr is the Unicode code points in chars. Let us discuss this concept with the help of an example:
Example:
// Go program to illustrate how to check the // string is present or not in the specified // string package main import ( "fmt" "strings") // Main function func main() { // Creating and initializing strings str1 := "Welcome to Geeks for Geeks" str2 := "Here! we learn about go strings" // Checking the string present or not // Using ContainsAny() function res1 := strings.ContainsAny(str1, "Geeks") res2 := strings.ContainsAny(str2, "GFG") res3 := strings.ContainsAny("GeeksforGeeks", "G & f") res4 := strings.ContainsAny("GeeksforGeeks", "u | e") res5 := strings.ContainsAny(" ", " ") res6 := strings.ContainsAny("GeeksforGeeks", " ") // Displaying the result fmt.Println("\nResult 1: ", res1) fmt.Println("Result 2: ", res2) fmt.Println("Result 3: ", res3) fmt.Println("Result 4: ", res4) fmt.Println("Result 5: ", res5) fmt.Println("Result 6: ", res6) } |
Output:
Result 1: true Result 2: false Result 3: true Result 4: true Result 5: true Result 6: false
Recommended Posts:
- How to count specific characters present in the slice in Golang?
- Check if the specified element is present in the slice of bytes in Golang
- Golang | Finding Index of the Regular Expression present in String
- Golang | Finding Index of the Regular Expression present in Slice
- How to Replace Characters in Golang String?
- How to Count the Number of Repeated Characters in a Golang String?
- Program to find the Length of the longest substring without repeating characters in Golang
- Check if the String starts with specified prefix in Golang
- Check if the String ends with specified suffix in Golang
- How to check String for equality under Unicode case folding in Golang?
- How to check the specified rune in Golang String?
- Check if the given slice is sorted in Golang
- How to check equality of slices of bytes in Golang?
- Check if the Slice of bytes starts with specified prefix in Golang
- Check if the Slice of bytes ends with specified suffix in Golang
- Check If the Rune is a Unicode Punctuation Character or not in Golang
- Check If the Rune is a Letter or not in Golang
- Check If the Rune is a Decimal Digit or not in Golang
- Check If the Rune is an Uppercase Letter or not in Golang
- Check If the Rune is a Lowercase Letter or not in Golang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

