一尘不染

如何在C#中用单个空格替换多个空格?

c#

如何用C#中的一个空格替换字符串中的多个空格?

例:

1 2 3  4    5

将会:

1 2 3 4 5

阅读 295

收藏
2020-05-19

共1个答案

一尘不染

string sentence = "This is a sentence with multiple    spaces";
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);     
sentence = regex.Replace(sentence, " ");
2020-05-19