views:

112

answers:

2

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?

+1  A: 

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);
shahkalpesh
String.Replace would not remove the item it would just remove the `--`
Binoj Antony
+5  A: 

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

Kobi