Removing extra commas from a string after using String.Join to convert an array to a string (C #)

I convert an array to a string using String.Join . I have a small problem in that in the array, some index positions will be empty. Example below:

 array[1] = "Firstcolumn" array[3] = "Thirdcolumn" 

Using String.Join (",", array); I will get the following:

Firstcolumn, Thirdcolumn

Pay attention to additional,.

How can I remove extra commas from a string or ideally not include empty indexes when using String.Join?

+49
string c # join
07 Oct '10 at
source share
9 answers

Try the following :):

 var res = string.Join(",", array.Where(s => !string.IsNullOrEmpty(s))); 

This will only connect to strings that are not null or "" .

+84
Oct 07 '10 at
source share

A simple solution would be to use linq, filtering out empty elements before joining.

 // .net 3.5 string.Join(",", array.Where(item => !string.IsNullOrEmpty(item)).ToArray()); 

In .NET 4.0, you can also use string.IsNullOrWhiteSpace if you also want to filter out elements that are empty or composed of (note that in .NET 4.0 you do not need to call ToArray in this case):

 // .net 4.0 string.Join(",", array.Where(item => !string.IsNullOrWhiteSpace(item))); 
+34
Oct 07 '10 at
source share

You can use linq to remove empty fields.

 var joinedString = String.Join(",", array.Where(c => !string.IsNullOrEmpty(c)); 
+3
Oct 07 '10 at
source share

Extension Method:

 public static string ToStringWithoutExtraCommas(this object[] array) { StringBuilder sb = new StringBuilder(); foreach (object o in array) { if ((o is string && !string.IsNullOrEmpty((string)o)) || o != null) sb.Append(o.ToString()).Append(","); } sb.Remove(sb.Length - 1, 1); return sb.ToString(); } 
+1
Oct 07 '10 at
source share

Regular expression:

 yourString = new Regex(@"[,]{2,}").Replace(yourString, @","); 
+1
07 Oct '10 at
source share
 String.Join(",", array.Where(w => !string.IsNullOrEmpty(w)); 
+1
Oct 07 '10 at
source share
 string.Join(",", Array.FindAll(array, a => !String.IsNullOrEmpty(a))); 

How about this? Cons and pros compared to LINQ? At least it's shorter.

0
Aug 24 '11 at 12:50
source share
 string.Join(",", string.Join(",", array).Split({","}, StringSplitOptions.RemoveEmptyEntries)); 

v ('_') B

0
Dec 05 '13 at 20:52
source share

Easy way to expand

 namespace System { public static class Extenders { public static string Join(this string separator, bool removeNullsAndWhiteSpaces, params string[] args) { return removeNullsAndWhiteSpaces ? string.Join(separator, args?.Where(s => !string.IsNullOrWhiteSpace(s))) : string.Join(separator, args); } public static string Join(this string separator, bool removeNullsAndWhiteSpaces, IEnumerable<string> args) { return removeNullsAndWhiteSpaces ? string.Join(separator, args?.Where(s => !string.IsNullOrWhiteSpace(s))) : string.Join(separator, args); } } } 

Using:

 var str = ".".Join(true, "a", "b", "", "c"); //or var arr = new[] { "a", "b", "", "c" }; str = ".".Join(true, arr); 
0
Oct. 25 '17 at 23:08 on
source share



All Articles