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?

3
  • 2
    God help the graduates of a university in which this question is homework. Commented Feb 16, 2011 at 5:19
  • possible duplicate of Convert a string array to a concantenated string in C# Commented Feb 16, 2011 at 5: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. Commented Feb 16, 2011 at 20:08

7 Answers 7

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);
Sign up to request clarification or add additional context in comments.

Comments

2
string result = string.Join("::", list.Where(s => !string.IsNullOrEmpty(s)).ToArray());

1 Comment

@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.
1
string.Join("::", strings.Where(item=>!string.IsNullOrEmpty(item)).ToArray()); 

Comments

0

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

2 Comments

The "or earlier" doesn't really apply because of the reliance on the Enumerable.ToArray method, which came out in 3.5.
@Ani - as long as the generic collections have been around, List<T> has had a ToArray method in it's signature. So as long as you're not using .NET 1.1, you should be ok.
0

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

Comments

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.

1 Comment

Interesting idea - do you think the overhead of ToArray is more significant than the overhead of a StringBuilder and then iterating through the collection?
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 ::)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.