How to remove a string?
I have an string like this:
string s1 = "abc,tom,--Abc, tyu,--ghh";
This string is dynamic, and I need to remove all substrings starting with "--"
.
s1 = "abc,tom, tyu";
How can开发者_高级运维 I remove these substrings?
Try:
Regex.Replace(s1, "--[^,]*,?", "");
This will search the string for blocks that start with --
, have some characters that are not commans (spaces or letter), and the comma (optional - there's no comma in the end).
Look at String.Replace
I am sorry, I should have read the question correctly. Regex comes to mind, for your case.
EDIT
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);
精彩评论