Search

Aug 7, 2008

Remove duplicate comma from list

Hi,

Recently on ASP.NET forum there was post for removing duplicate ',' from a comma saperated string.

This is using Regex
string abodes = ",House,,,,Apartment,,,Villa,,,,Townhouse,,,,";
Regex regx = new Regex(@",+");
abodes = regx.Replace(abodes, ",").Trim(',');
And now let's see using LINQ
string abodes = ",House,,,,Apartment,,,Villa,,,,Townhouse,,,,"; 
string cleanAbodes = string.Join(",", abodes.Split(',').Where(a => a != string.Empty).ToArray());
Have Fun with LINQ

No comments: