How to check if a string contains a white space in Golang?
Checking if a string contains a space returns True if there is at least one occurrence of " " within the string. For example, checking if "Go Language" contains a space returns True.
Use Regex to verify a string contains white-space
// Golang program to illustrate
// how to check is string
// contains white-space using regex
package main
import (
"fmt"
"regexp"
)
func main() {
word := "Go Language"
whitespace := regexp.MustCompile(`\s`).MatchString(word)
fmt.Println(whitespace)
}
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 create a photo gallery in Go?
How to read current directory using Readdir?
How to iterate over a Map using for loop in Go?
Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of Slices
How to create Map using the make function in Go?
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?
Sample program to create csv and write data
How to find the type of the variable by different ways in Golang?
Anonymous Functions in Golang
Example: Fields and FieldsFunc from BYTES Package