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:
package main
import (
"fmt"
"strings"
)
func main() {
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)
res1 := strings.Contains(str1, "Geeks" )
res2 := strings.Contains(str2, "GFG" )
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:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "Welcome to Geeks for Geeks"
str2 := "Here! we learn about go strings"
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" , " " )
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