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"