Two Dimensional Array C# -
i input user - "99211,99212,99213_1,99214,99215_3" , store in string as
string cpt = "99211,99212,99213_1,99214,99215_3"; cptarray = cpt.split(',');
i got output
cptarray[0] = "99211" cptarray[1] = "99212" cptarray[2] = "99213_1" cptarray[3] = "99214" cptarray[4] = "99215_3"
but output be:
cptarray[0][0] = "99211","" cptarray[1][0] = "99212","" cptarray[2][0] = "99213","1" cptarray[3][0] = "99214","" cptarray[4][0] = "99215","3"
if need output above can use 2d array, correct approach?
according syntax provided:
cptarray[0][0] ... cptarray[4][0]
you want jagged array, not 2d one; can construct array of linq:
var cptarray = cpt .split(',') .select(number => number.split('_')) .select(items => items.length == 1 ? new string[] {items[0], ""} : items) .toarray();
test
string test = string.join(environment.newline, cptarray .select((line, index) => string.format("cptarray[{0}] = {1}", index, string.join(", ", line.select(item => "\"" + item + "\""))))); console.write(test);
output
cptarray[0] = "99211", "" cptarray[1] = "99212", "" cptarray[2] = "99213", "1" cptarray[3] = "99214", "" cptarray[4] = "99215", "3"
Comments
Post a Comment