小编典典

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

all

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

例子:

1 2 3  4    5

将是:

1 2 3 4 5

阅读 101

收藏
2022-03-11

共1个答案

小编典典

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