Logo

Programming-Idioms

History of Idiom 219 > diff from v26 to v27

Edit summary for version 27 by zqwnvl:
New C# implementation by user [zqwnvl]

Version 26

2021-08-19, 07:46:46

Version 27

2021-08-19, 07:50:27

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
using System.Text.RegularExpressions;
Code
string t = Regex.Replace(s, @"\s+", " ");
Comments bubble
Replaces consecutive whitespace with a space. Verbatim string used to avoid escaping backslash.
Doc URL
https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions
Demo URL
https://sharplab.io/#gist:4ab8a6e45f019c86330a2cac655fa10a