2

The List<string> has ("ABC","","DEF","","XYZ"), how can I get the string "ABC::DEF::XYZ" out of the List in C#?

ADDED

List<string> strings = new List<string> {"ABC","","DEF","","XYZ"};
string joined = string.Join("::", strings.ToArray());
Console.WriteLine(joined);

gives ABC::::DEF::::XYZ, not ABC::DEF::XYZ, how can one skip the empty strings ("") in a list?

prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 2
    God help the graduates of a university in which this question is homework. – mqp Feb 16 '11 at 05:19
  • possible duplicate of [Convert a string array to a concantenated string in C#](http://stackoverflow.com/questions/1304981/convert-a-string-array-to-a-concantenated-string-in-c) – Kirk Broadhurst Feb 16 '11 at 05:24
  • do you really want valid strings separated by double colons or all strings separated by single colons? Your output example could be either. It seems to me more useful in reconstituting the list if it's all strings separated by single colons -- that way you can get the empty strings back. – tvanfosson Feb 16 '11 at 20:08

7 Answers7

7

You can do:

List<string> strings = ...
string joined = string.Join(":", strings.ToArray());

In .NET 4.0, you can leave out the ToArray() call.

EDIT: Based on your update that indicates that you want to skip empty strings and use two colons as the delimiter, you can do:

// Use !string.IsNullOrEmpty or !string.IsNullOrWhiteSpace if more appropriate.   
string[] filtered = list.Where(s => s != string.Empty) 
                        .ToArray();

string joined = string.Join("::", filtered);
Ani
  • 111,048
  • 26
  • 262
  • 307
2
string result = string.Join("::", list.Where(s => !string.IsNullOrEmpty(s)).ToArray());
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • @tvanfosson - Did not see those; edited. I'm wondering if the question was edited after posting, since some other answerers seem not to have accounted for those either. – Justin Morgan - On strike Feb 16 '11 at 05:39
1
string.Join("::", strings.Where(item=>!string.IsNullOrEmpty(item)).ToArray()); 
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
0

string.Join( ":", list ) in .NET 4.0. If you are using 3.5 or earlier, string.Join( ":", list.ToArray() )

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • The "or earlier" doesn't really apply because of the reliance on the Enumerable.ToArray method, which came out in 3.5. – Ani Feb 16 '11 at 05:26
  • @Ani - as long as the generic collections have been around, `List` has had a `ToArray` method in it's signature. So as long as you're not using .NET 1.1, you should be ok. – tvanfosson Feb 16 '11 at 20:04
0

You should look at String.Join(), example String.Join(":",list.ToArray());

Zachary
  • 6,522
  • 22
  • 34
0

Using string.Join with ToArray would work.

As Ani said, if you're on .NET 4.0, you could leave out the ToArray.

If you're not on .NET 4.0 but you don't want the overhead of the ToArray call, you could write a method to create a StringBuilder, append every item in the List<string> plus your delimiter (skipping the delimiter after the last item), and return the result of ToString at the end.

Community
  • 1
  • 1
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • Interesting idea - do you think the overhead of `ToArray` is more significant than the overhead of a `StringBuilder` and then iterating through the collection? – Kirk Broadhurst Feb 16 '11 at 05:37
0

I think you can have a look at The suggested method

post this you can simply do string.Join(",", strings.ToArray())

(replace the empty strings with ::)

Community
  • 1
  • 1
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82