How to check if a string contains certain characters in Golang?
Checking if a string contains certain characters returns True if the string is composed of only the specified characters and False otherwise.
The Contains function from strings package is used to check the given characters present in the given string or not. If the character is present in the given string, then it will return true, otherwise, return false.
Check characters example with boolean output
// Golang program to illustrate
// the strings.Contains() Function
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Contains("abcd", "b")) // true
fmt.Println(strings.Contains("abcd", "cb")) // false
}
Check characters example to print the desired result instead of a boolean output
package main
import (
"fmt"
"strings"
)
func main() {
input := "p"
str := "Apple"
if strings.Contains(str, input) {
fmt.Println("Yes")
}
}
Most Helpful This Week
How to remove multiple spaces in a string in GoLang?
How to trim leading and trailing white spaces of a string in Golang?
Strip all white spaces, tabs, newlines from a string
Golang String Concatenation
How to remove all line breaks from a string in Golang?
How to check if a string contains a substring in Golang?
How to check string contains uppercase lowercase character in Golang?
How to remove special characters from a string in GoLang?
How to check if a string contains a numbers in Golang?
How to check if a string contains a white space in Golang?