Logo

Programming-Idioms

History of Idiom 219 > diff from v25 to v26

Edit summary for version 26 by zqwnvl:
[C#] Replace entire sequence instead of only pairs; add doc link

Version 25

2021-08-16, 02:19:01

Version 26

2021-08-19, 07:46:46

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
Imports
using System.Text.RegularExpressions;
Code
var t = s.Replace("  ", " ");
Code
string t = Regex.Replace(s, " +", " ");
Comments bubble
Will replace all instances of two spaces in a string.
Comments bubble
Replaces consecutive spaces with a single space.
Doc URL
https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions
Origin
https://dotnetfiddle.net/v5POhA
Demo URL
https://dotnetfiddle.net/v5POhA
Demo URL
https://sharplab.io/#gist:831ac4f3a70b2c757db102558bc84176