How to join int[] to a character separated string in .NET?

后端 未结 11 813
广开言路
广开言路 2020-11-29 19:17

I have an array of integers:

int[] number = new int[] { 2,3,6,7 };

What is the easiest way of converting these into a single string where

相关标签:
11条回答
  • 2020-11-29 19:42

    Although the OP specified .NET 3.5, people wanting to do this in .NET 2.0 with C#2 can do this:

    string.Join(",", Array.ConvertAll<int, String>(ints, Convert.ToString));
    

    I find there are a number of other cases where the use of the Convert.xxx functions is a neater alternative to a lambda, although in C#3 the lambda might help the type-inferencing.

    A fairly compact C#3 version which works with .NET 2.0 is this:

    string.Join(",", Array.ConvertAll(ints, item => item.ToString()))
    
    0 讨论(0)
  • 2020-11-29 19:42
    ints.Aggregate("", ( str, n ) => str +","+ n ).Substring(1);
    

    I also thought there was a simpler way. Don't know about performance, anyone has any (theoretical) idea?

    0 讨论(0)
  • 2020-11-29 19:44
    String.Join(";", number.Select(item => item.ToString()).ToArray());
    

    We have to convert each of the items to a String before we can join them, so it makes sense to use Select and a lambda expression. This is equivalent to map in some other languages. Then we have to convert the resulting collection of string back to an array, because String.Join only accepts a string array.

    The ToArray() is slightly ugly I think. String.Join should really accept IEnumerable<String>, there is no reason to restrict it to only arrays. This is probably just because Join is from before generics, when arrays were the only kind of typed collection available.

    0 讨论(0)
  • 2020-11-29 19:49

    The question is for "easiest way of converting these in to a single string where the number are separated by a character".

    The easiest way is:

    int[] numbers = new int[] { 2,3,6,7 };
    string number_string = string.Join(",", numbers);
    // do whatever you want with your exciting new number string
    

    EDIT: This only works in .NET 4.0+, I missed the .NET 3.5 requirement the first time I read the question.

    0 讨论(0)
  • 2020-11-29 19:51

    I agree with the lambda expression for readability and maintainability, but it will not always be the best option. The downside to using both the IEnumerable/ToArray and StringBuilder approaches is that they have to dynamically grow a list, either of items or characters, since they do not know how much space will be needed for the final string.

    If the rare case where speed is more important than conciseness, the following is more efficient.

    int[] number = new int[] { 1, 2, 3, 4, 5 };
    string[] strings = new string[number.Length];
    for (int i = 0; i < number.Length; i++)
      strings[i] = number[i].ToString();
    string result = string.Join(",", strings);
    
    0 讨论(0)
  • 2020-11-29 19:52

    You can do

    ints.ToString(",")
    ints.ToString("|")
    ints.ToString(":")
    

    Check out

    Separator Delimited ToString for Array, List, Dictionary, Generic IEnumerable

    0 讨论(0)
提交回复
热议问题