开发者

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 "--".

Output for the example string:

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜