Software Engineering Wiki

CompactWhiteSpace

Returns a string with whitespaces compacted.

  • Use Regexp.ReplaceAllString() with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space.
import "regexp"

func CompactWhiteSpace(str string) string {
	re := regexp.MustCompile(`\s{2,}`)
	return re.ReplaceAllString(str, " ")
}
CompactWhiteSpace("Lorem    Ipsum") // "Lorem Ipsum"
CompactWhiteSpace("Lorem \n Ipsum") // "Lorem Ipsum"

Last updated 15 September 2026 · Edit this page