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
  • You should state how the ends of the substrings are marked. It appears to be with either a comma or end of string, but you should really say that in your question. Commented Jan 18, 2010 at 6:44

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

String.Replace would not remove the item it would just remove the --

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.