Logo

Programming-Idioms

History of Idiom 219 > diff from v17 to v18

Edit summary for version 18 by andyjduncan:
New Groovy implementation by user [andyjduncan]

Version 17

2020-05-03, 19:34:53

Version 18

2020-10-10, 11:18:47

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Extra Keywords
collapse repeated
Extra Keywords
collapse repeated
Code
def t = s.replaceAll(/\s+/, ' ')
Comments bubble
Replaces all whitespace. Uses Groovy's slashy string syntax to avoid escaping the whitespace character class.
Doc URL
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/CharSequence.html#replaceAll(java.util.regex.Pattern,%20java.lang.CharSequence)
Demo URL
https://groovyconsole.appspot.com/script/5172018601263104