Wednesday, 15 January 2014

c# - Converting a string array to an integer array -



c# - Converting a string array to an integer array -

i reading text file using c#.net v3.5 express 2010 containing integers in format

18 11 2 18 3 14 1 0 1 3 22 15 0 6 8 23 18 1 3 4 10 15 24 17 17 16 18 10 17 18 23 17 11 19

by

string[] code = system.io.file.readalllines(@"c:\randoms\randnum.txt");

i placethis string by

string s1 = convert.tostring(code);

and need able read int array mathematical processing.

i've tried suggested on site under other posts on topic, including parsing, , covert array 1 time effort dreaded "input string not in right format" message

you can utilize linq:

var ints = code.selectmany(s => s.split(' ')).select(int.parse).tolist();

this take list of space-separated numbers , flatten them one-dimensional list of ints

c# .net arrays string linq

No comments:

Post a Comment