What's the most simple way to convert comma separated string to int[]?

88,811

Solution 1

string s = "1,5,7";
int[] nums = Array.ConvertAll(s.Split(','), int.Parse);

or, a LINQ-y version:

int[] nums = s.Split(',').Select(int.Parse).ToArray();

But the first one should be a teeny bit faster.

Solution 2

string numbers = "1,5,7";
string[] pieces = numbers.Split(new string[] { "," },
                                  StringSplitOptions.None);

int[] array2 = new int[pieces.length];

for(int i=0; i<pieces.length; i++)
    array2[i] = Convert.ToInt32(pieces[i]);

Solution 3

Here you go.

string numbers = "1,5,7";
List<int> numlist = new List<int>();

foreach (string number in numbers.Split(','))
    numlist.Add(Int32.Parse(number));
Share:
88,811
kseen
Author by

kseen

Software development engineer. Feel free to contact me ksninv at gmail.

Updated on September 20, 2020

Comments

  • kseen
    kseen over 3 years

    So I have comma-separated string like 1,5,7, so what's the most simple and native way to convert this string to int[]? I can write my own split function, but there's some interest how to do it in most native way. Thanks in advance guys!

  • Nahydrin
    Nahydrin almost 13 years
    Wow, you learn something new everyday... o_o
  • Yola
    Yola over 5 years
    Why the first one is faster?
  • Ben Voigt
    Ben Voigt over 5 years
    @Yola: Because Array.ConvertAll knows what size to make the output -- the LINQ ToArray() function has to collect the results without knowing the count in advance.
  • stephanmg
    stephanmg about 5 years
    Will this also be fast for very large strings? Let's say strings of 1,000,000 characters?
  • stephanmg
    stephanmg about 5 years
    Why the new string[] { "," }? numbers.Split(",") should be enough?
  • Ben Voigt
    Ben Voigt about 5 years
    @stephanmg: For 1 million, it should be fast. As you start getting to the point where you can't fit two copies of the data into memory, a streaming method will be worth the extra complexity.
  • stephanmg
    stephanmg about 5 years
    But 1 million characters are only like 50 MB of memory?
  • Ben Voigt
    Ben Voigt about 5 years
    @stephanmg: 1 million characters should be about 2MB of memory, which is no issue at all. Your example of "very large" was not in fact all that large by the standards of today's computers. When you get close to a billion characters, the Split approach is going to start being a problem. (For context, the Bible is roughly 3 million characters, and will be processed easily in memory. The Encyclopedia Britannica is 44 million words, so several hundred million characters, and still processed in memory but starting to approach the limit. Every book on one library rack would be too much.)
  • stephanmg
    stephanmg about 5 years
    Yeah on my computer the Python split method will take 1 seconds for strings of 200 MB roughly. Is that already considered slow? In principle I will need to split a string of 100 MB (CSV string) in soft real time... 500 ms maybe max in C#. Any suggestions? Maybe C#'s split function will do it? Need to test this.
  • Ben Voigt
    Ben Voigt about 5 years
    @stephanmg: Where are you getting new data of 100 MB in less than 50ms? That's 16 Gbps!
  • stephanmg
    stephanmg about 5 years
    Well a 0 is missing. Sorry. I create dummy data within a python script.
  • Ben Voigt
    Ben Voigt about 5 years
    @stephanmg: For that data rate, C# string.Split might keep up, and might not. I would definitely suggest trying it before turning to more efficient but lower-level/higher programmer effort tools such as parser generators or streaming parser templates available in C++.
  • stephanmg
    stephanmg about 5 years
    I agree @BenVoigt. I will try it out. I can get my data also as base64 encoded string (which encodes the long CSV string in base64). Would parsing that be more efficient than string splitting? Pardon me if this is a naive or trivial question.
  • Rizkiramadhani
    Rizkiramadhani over 4 years
    Gooooddd jooobbb!!!!