How to check if a string contains a numbers in Golang?
A string contains a number if any of the characters are digits (0-9).
Using Regex to check if a string contains digit
package main
import (
"fmt"
"regexp"
)
func main() {
word := "test25"
numeric := regexp.MustCompile(`\d`).MatchString(word)
fmt.Println(numeric)
}
Regular expression is widely used for pattern matching. The regexp package provides support for regular expressions, which allow complex patterns to be found in strings. The regexp.MustCompile() function is used to create the regular expression and the MatchString() function returns a bool that indicates whether a pattern is matched by the string.
Most Helpful This Week
How to check if a string contains only letters in Golang?
How to check if a string contains a white space in Golang?
How to check UPPERCASE characters in a string in Golang?
Golang String Concatenation
How to remove multiple spaces in a string in GoLang?
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?