Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove extra commas in C#

Tags:

c#-3.0

I have a string like "abc,,bcd,";

The output should be abc,bcd i.e. the extra commas should be removed.

Help needed


2 Answers

string result = Regex.Replace(input, ",+", ",").Trim(',');
like image 150
mmx Avatar answered Sep 09 '25 02:09

mmx


How about something like

string s = "abc,,bcd,";
s = s.Trim(',');
while (s.Contains(",,"))
    s = s.Replace(",,", ",");
like image 30
Adriaan Stander Avatar answered Sep 09 '25 01:09

Adriaan Stander