c# - Why did System.String[] appear as the output from printing 2d array? -
using this, i'm trying print list of contents of players.text know foreach loop print fine if console.writeline used. i'm attempting manipulate data in particular ways later, , need split using indicated delimiter.
however, given code repeated system.string[] printout.
i've done search or 2 , of i've found has 1 part or other, haven't found information on how use them together.
string[] playerfile = file.readalllines("players.txt"); foreach (string s in playerfile) { //console.writeline(s); string[] playerstuff = s.split(';'); console.writeline(playerstuff); } console.readkey();
i realize it's simplistic question. often, me @ least, it's missing obvious drives me crazy.
thanks in advance.
player;team;pos;hr;rbi;avg
abreu, j;cws;1b;30;101;0.29
altuve, j;hou;2b;15;66;0.313
andrus, e;tex;ss;7;62;0.258
arenado, n;col;3b;42;130;0.287
aybar, e;laa;ss;3;44;0.27
the above first few lines of input.
basically, want that, minus semicolons. formatting come later.
attempting add second foreach, such suggested below, code looked this:
foreach (string s in playerfile) { //console.writeline(s); string[] playerstuff = s.split(';'); foreach (string player in playerstuff) { console.writeline(player); } }
resulted in each piece of information getting it's own line. follow logic of why did that, i'm not sure it.
the reason getting output because trying write array
object console; , doesn't know how handle it, printing out type
of object trying write out.
you need foreach
or similar on playerstuff.
string[] playerfile = file.readalllines("players.txt"); foreach (string s in playerfile) { //console.writeline(s); string[] playerstuff = s.split(';'); //inner foreach writes out each entry of array foreach(var item in playerstuff) { console.write(item + " "); } console.write(environment.newline); } console.readkey();
or simpler
string[] playerfile = file.readalllines("players.txt"); foreach (string s in playerfile) { console.writeline(s.replace(';', ' ')); } console.readkey();
or 1 liner
string[] playerfile = file.readalllines("players.txt"); playerfile.foreach(x => console.writeline(x.replace(';', ' '))); console.readkey();
sample output image 3 of above solutions:
console.writeline docs reference
string.replace docs reference
Comments
Post a Comment