如何将“ a b c ”修剪成“a b c”

32 浏览
0 Comments

如何将“ a b c ”修剪成“a b c”

这个问题已经有了答案:

可能是重复的问题:

如何在C#中用最优雅的方式修剪字符串中的空格,例如\"a<很多空格>b c\"变成\"a b c\"。因此,重复的空格被缩小成一个空格。

admin 更改状态以发布 2023年5月24日
0
0 Comments

你可以使用正则表达式来实现这个功能:\n

Regex.Replace(my_string, @"\s+", " ").Trim();

0
0 Comments

一种没有使用正则表达式的解决方案,只是为了将其记录在表格中: \n

char[] delimiters = new char[] { ' '};   // or null for 'all whitespace'
string[] parts = txt.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
string result = String.Join(" ", parts);

0