Check if the given characters is present in Golang String
Last Updated :
26 Aug, 2019
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:
C
// 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:
CSharp
// 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
Similar Reads
Check If the Rune is a Space Character or not in Golang Rune is a superset of ASCII or it is an alias of int32. It holds all the characters available in the world's writing system, including accents and other diacritical marks, control codes like tab and carriage return, and assigns each one a standard number. This standard number is known as a Unicode c
2 min read
Check If the Rune is a Symbolic Character or not in Golang Rune is a superset of ASCII or it is an alias of int32. It holds all the characters available in the world's writing system, including accents and other diacritical marks, control codes like tab and carriage return, and assigns each one a standard number. This standard number is known as a Unicode c
1 min read
How to Replace Characters in Golang String? 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 replace characters in the given string usin
4 min read
How to Generate Random String/Characters in Golang? We might want to generate random strings or even sets of characters to perform some operations or add certain string-related functionality into an application. We can randomly get a character from a set of characters, randomize the order of characters of a given string or generate a random string. W
6 min read
Check If the Rune is a Letter or not in Golang Rune is a superset of ASCII or it is an alias of int32. It holds all the characters available in the world's writing system, including accents and other diacritical marks, control codes like tab and carriage return, and assigns each one a standard number. This standard number is known as a Unicode c
2 min read