Logo

Programming-Idioms

History of Idiom 219 > diff from v28 to v29

Edit summary for version 29 by zqwnvl:
New Java implementation by user [zqwnvl]

Version 28

2021-08-19, 08:11:50

Version 29

2021-08-19, 08:13:32

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
String t = s.replaceAll("\\s+", " ");
Comments bubble
Replaces consecutive whitespace with a space.
Doc URL
https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/String.html#replaceAll(java.lang.String,java.lang.String)
Demo URL
https://ideone.com/h29Bjr