我有一个这样的字符串:
string s1 = "abc,tom,--Abc, tyu,--ghh";
此字符串是动态的,我需要删除所有以"--"
开头的子字符串
示例字符串的输出:
s1 = "abc,tom, tyu";
如何删除这些子字符串?
答案 0 :(得分:5)
尝试:
Regex.Replace(s1, "--[^,]*,?", "");
这将在字符串中搜索以--
开头的块,包含一些不是commans(空格或字母)的字符,以及逗号(可选 - 最后没有逗号)。
答案 1 :(得分:1)
对不起,我应该正确地阅读这个问题。 根据您的情况,可以想到正则表达式。
修改
<强> LINQ 强>
string s1 = "abc,tom,--Abc, tyu,--ghh";
var s2 = s1
.Split(',')
.Where(s => s.StartsWith("--") == false)
.Aggregate((start, next) => start + "," + next);
Console.WriteLine(s2);