c# - Splitting a string seems not to work -
i have problems reading file (textasset) line line , getting results!
here file trying read:
author comment info 1 x arg 0001 0.581 2.180 1.470 info 2 x arg 0001 1.400 0.974 1.724 info 3 x arg 0001 2.553 0.934 0.751 info 4 x arg 0001 3.650 0.494 1.053 info 5 x arg 0001 1.188 3.073 1.532 info 6 x arg 0001 2.312 1.415 -0.466 info 7 x arg 0001 -0.232 2.249 2.180 end
here code using:
//read file string[] line = file.text.split("\n"[0]); for(int = 0 ; < line.length ; i++) { if(line[i].contains("info")) { //to replace spaces single underscore "_" (it works fine) string l = regex.replace(line[i]," {2,}","_"); //in debug.log correct results //for example "info_1_x_arg_0001_0.581_2.180_1.470" debug.log(l); string[] subline = regex.split(l,"_"); //only first "info" line correct results (info,1,x,arg,0001,0.581,2.180,1.470) //for other "info" lines incomplete results (first,fourth , fifth element not put substrings //like dissapeard! foreach(string s in subline){debug.log(s);} } }
explanation:
i first split text lines (works fine),then read lines contain info
i loop lines contain info
, replace spaces underscore _
(this works fine)
i split lines contain info
substrings based on underscore _
when print out lines first line info
seems have substrings every next line not splitted correctly (first part info
omitted third string)
it seems unreliable. way go these things? appreciated! should simple, doing wrong?
edit:
something wrong code (it should simple, not work)
here updated code (i made list<string> list = new list<string>()
, copied substrings. use unity3d list contents show in inspector. shocked when extracted substrings simple
foreach(string s in list) debug.log(s);
was indeed missing values. trying different things , code:
for(int x = 0; x < list.count ; x++) { debug.log("list: " + x.tostring() + " " + list[x].tostring()); }
shows contents of list properly, code (note removed x.tostring()
) missing elements in list. not want read them!
for(int x = 0; x < list.count ; x++) debug.log("list: " + list[x].tostring());
so not sure going on here?!
there problems
1>the contains method using case sensitive i.e info != info
you should use
line[i].tolower().contains("info")
2>is text separated space.it may separated tabs.you better off with
regex.replace(line[i]," {2,}|\t+","_"); //this replace 1 many tabs or 2 or more space
Comments
Post a Comment