Software Engineering Wiki

ContainsWhiteSpace

Returns a string with whitespaces compacted.

  • Use Regexp.MatchString() with a regular expression to check if the given string contains any whitespace characters.
import "regexp"

func ContainsWhiteSpace(str string) bool {
	re := regexp.MustCompile(`\s`)
	return re.MatchString(str)
}
ContainsWhiteSpace("lorem") // false
ContainsWhiteSpace("lorem ipsum") // true

Last updated 15 September 2026 · Edit this page