更新时间:2023-11-30 22:45:22
可以用一行重新编写用单个空格替换空格序列的函数:
Your function that replaces sequences of spaces with a single space can be re-written in a single line:
string.Join(" ", xString.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries);
如果要使其成为扩展方法,则可以这样进行:
If you would like to make it an extension method, you can do it like this:
static class StringExtensions {
public static string RemoveExtraSpaces(this string xString) {
return string.Join(" ", xString.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries));
}
}