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));
Author by
kseen
Software development engineer. Feel free to contact me ksninv at gmail.
Updated on September 20, 2020Comments
-
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 thisstring
toint[]
? 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 almost 13 yearsWow, you learn something new everyday... o_o
-
Yola over 5 yearsWhy the first one is faster?
-
Ben Voigt over 5 years@Yola: Because
Array.ConvertAll
knows what size to make the output -- the LINQToArray()
function has to collect the results without knowing the count in advance. -
stephanmg about 5 yearsWill this also be fast for very large strings? Let's say strings of 1,000,000 characters?
-
stephanmg about 5 yearsWhy the new string[] { "," }? numbers.Split(",") should be enough?
-
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 about 5 yearsBut 1 million characters are only like 50 MB of memory?
-
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 about 5 yearsYeah 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 about 5 years@stephanmg: Where are you getting new data of 100 MB in less than 50ms? That's 16 Gbps!
-
stephanmg about 5 yearsWell a 0 is missing. Sorry. I create dummy data within a python script.
-
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 about 5 yearsI 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 over 4 yearsGooooddd jooobbb!!!!