Chris
Chris

Reputation: 7611

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

I'm converting an array into a string using String.Join. A small issue I have is that, in the array some index positions will be blank. An example is below:

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

By using String.Join(",", array);, I'll get the following:

Firstcolumn,,Thirdcolumn

Note the extra ,.

How can I remove the extra commas from the string, or ideally not include blank indices when using String.Join?

Upvotes: 50

Views: 14406

Answers (9)

DATEx2
DATEx2

Reputation: 3508

Simple extension method

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

Usage:

var str = ".".Join(true, "a", "b", "", "c");
//or 
var arr = new[] { "a", "b", "", "c" };
str = ".".Join(true, arr);

Upvotes: 0

Lasse Espeholt
Lasse Espeholt

Reputation: 17782

Try this :):

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

This will join only the strings which is not null or "".

Upvotes: 88

Sam
Sam

Reputation: 1

string.Join(",", string.Join(",", array).Split({","}, StringSplitOptions.RemoveEmptyEntries));

v('_')V

Upvotes: -1

Denis
Denis

Reputation: 3757

string.Join(",", Array.FindAll(array, a => !String.IsNullOrEmpty(a)));

How about this one? Cons and pros comparing to LINQ solution? At least it shorter.

Upvotes: 0

bernhof
bernhof

Reputation: 6320

A simple solution would be to use linq, by filtering out the empty items before joining.

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

In .NET 4.0 you could also make use of string.IsNullOrWhiteSpace if you also want to filter out the items that are blank or consist of white space characters only (note that in .NET 4.0 you don't have to call ToArray in this case):

// .net 4.0
string.Join(",", array.Where(item => !string.IsNullOrWhiteSpace(item)));

Upvotes: 34

Even Mien
Even Mien

Reputation: 45898

Regular expression solution:

yourString = new Regex(@"[,]{2,}").Replace(yourString, @",");

Upvotes: 1

Richard J. Ross III
Richard J. Ross III

Reputation: 55573

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

Upvotes: 1

Julien Hoarau
Julien Hoarau

Reputation: 49970

String.Join(",", array.Where(w => !string.IsNullOrEmpty(w));

Upvotes: 1

Andy Rose
Andy Rose

Reputation: 16984

You could use linq to remove the empty fields.

var joinedString = String.Join(",", array.Where(c => !string.IsNullOrEmpty(c));

Upvotes: 3

Related Questions