简体   繁体   中英

Comma Separated string Built In .NET

I used a built in function to create comma separated string using List easily. (It is not split and join but new function) I'm not able to recollect or find it. If some one knows about it and uses it please post a link to that. Framework - .net 2.0

(It is not Join or split - I know about this, .net has new built in function to create CSV format)

Check Jacob G Answer below for what I was looking for let me know your thoughts on it compared to join ;)

And whoever gave me -ve rep need to keep some patience and not hurry

public static string SomethingElseWithComma(this IEnumerable<string> list)
{
  if(list == null)
      return null;

  return String.Join(",",list.ToArray());
}

ps. don't downvote, just having fun.

This might be what you're thinking of... You need to reference the System.Configuration dll and import the appropriate namespace.

    List<string> temp = new List<string>();
    temp.Add("a");
    temp.Add("b");
    temp.Add("c");
    CommaDelimitedStringCollection cdsc = new CommaDelimitedStringCollection();
    cdsc.AddRange(temp.ToArray());
    Console.WriteLine(cdsc.ToString());

By the way, I found this class by opening up the documentation and typing the word "comma" in the index.

EDIT
In response to your new question - Assuming that your List is already constructed, String.Join is going to be more performant. This collection just uses a StringBuilder. String.Join has a number of low-level optimization that will make it faster.

(also, not terribly cool to take away the "correct answer" after you change to a new question)

Out of the box, I don't think List<T> has any methods or properties which do this. I agree with jsmith it must have been an extension method, etc.

//likely the best you'll do without writing your 
//own extension method or coding SomethingElse.
string.Join(", ", list.ToArray());

我相信您正在寻找String.Join方法?

String.Join(",",yourEnumerable.ToArray())

was it string.Join? MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM