c# - 从字符串数组的末尾删除所有空元素
问题描述
我有以下代码:
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()
,我感觉不太好。
解决方案
推荐阅读
- javascript - CORS 错误:使用 res.redirect() 时没有“Access-Control-Allow-Origin”标头
- django - 在模板标签过滤器Django中请求cart_obj
- android - 如何修复此错误,当我尝试从 react-native 项目构建 .apk 时发生
- python - 为 df2 pandas 数据框中的每个行条目添加计算列到 df2
- javascript - 如何将 DOM 事件作为事件处理程序绑定方法传递
- swift - 如何在 Swift 中将两个 UIImage 组合成一个图像?
- c++ - 为什么在虚函数声明中使用不同的返回类型会引发错误而不是导致重新定义?
- typescript - 如何为打字稿中的所有类方法声明一种类型
- angular - 如何用角材料更新表格中的一行?
- python - 插入 PostgreSQL 时出现“TypeError:字符串格式化期间并非所有参数都转换”