Logo

Programming-Idioms

History of Idiom 219 > diff from v18 to v19

Edit summary for version 19 by Zso:
New Rust implementation by user [Zso]

Version 18

2020-10-10, 11:18:47

Version 19

2020-11-17, 00:43:26

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
Imports
use regex::Regex;
Code
let re = Regex::new(r"\s+").unwrap();
let s2 = re.replace_all(s1, " ");
Doc URL
https://docs.rs/regex/1.4.2/regex/#example-replacement-with-named-capture-groups
Demo URL
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c1fac19554db5841f6b4caf4b9919943