C#LINQ去掉数组字符串中的指定元素
例字符串:
string s1 = "111,111,111222111,111333111,111";
string del = "111";
要删除指定元素“111”,得到
s1 = "111222111,111333111";
方法一) 传统Replace
1)将每个元素保留左右各一个分隔符,前后加一个特殊符号
s1 ="$,"+string.Join(",,", s1.Split(',').ToArray())+",$"; //得到:$,111,,111,,111222111,,111333111,,111,$
2)replace指定元素,并去掉前后特殊符号
s1 = s1.Replace("," + del+ ",", "").Replace("$,", "").Replace(",$", "").Replace("$$", "");
方法二)LinQ
1)需要添加引用
using System.Linq;
2)使用Where关键字过滤
s1 = string.Join(",", s1.Split(',').Where(d => d != del).ToArray());