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!
Asked
Active
Viewed 1.1e+01k times
3 Answers
111
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.

SavoryBytes
- 35,571
- 4
- 52
- 61

Ben Voigt
- 277,958
- 43
- 419
- 720
-
6Wow, you learn something new everyday... o_o – Nahydrin Jul 05 '11 at 05:18
-
1Why the first one is faster? – Yola Jan 08 '19 at 11:19
-
2@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. – Ben Voigt Jan 08 '19 at 14:39
-
Will this also be fast for very large strings? Let's say strings of 1,000,000 characters? – stephanmg Apr 19 '19 at 03:56
-
@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. – Ben Voigt Apr 19 '19 at 05:55
-
But 1 million characters are only like 50 MB of memory? – stephanmg Apr 19 '19 at 15:13
-
2@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.) – Ben Voigt Apr 19 '19 at 15:34
-
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. – stephanmg Apr 19 '19 at 17:14
-
@stephanmg: Where are you getting *new data* of 100 MB in less than 50ms? That's 16 Gbps! – Ben Voigt Apr 19 '19 at 17:16
-
Well a 0 is missing. Sorry. I create dummy data within a python script. – stephanmg Apr 19 '19 at 17:18
-
@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++. – Ben Voigt Apr 19 '19 at 20:44
-
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. – stephanmg Apr 19 '19 at 22:20
9
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]);

MD Sayem Ahmed
- 28,628
- 27
- 111
- 178
6
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));

Nahydrin
- 13,197
- 12
- 59
- 101