首页 > 解决方案 > 从字符串数组的末尾删除所有空元素

问题描述

我有以下代码:

public static string[] Split(this string me, string splitter, bool remove_empty_elements = false)
{
    return me.Split(new string[] {splitter}, remove_empty_elements ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
}

"ABCddcddd".Split("d")

它会生成一个字符串数组,如下所示:

╭─────┬─────────┬───┬─────────┬─────────┬─────────╮
│ ABC │ <empty> │ c │ <empty> │ <empty> │ <empty> │
╰─────┴─────────┴───┴─────────┴─────────┴─────────╯

从末尾删除所有空字符串元素并保留中间元素的最佳方法是什么?现在我正在做"ABCddcddd".Split("d").Reverse().SkipWhile(x => x=="").Reverse(),我感觉不太好。

标签: c#loopsiteration

解决方案


推荐阅读