When reading txt files in C #, when the delimiter of the data in TXT is a space, and the number of spaces is not equal, if you directly separate each row with split, then the number of lists is more than the number of columns per row, not the actual number of columns per row. At this time to the unequal number of spaces into 1 spaces, so that the count is correct. The code is as follows:
Public Static list<string>split (string s,char splitchar=") { Return S.split (Splitchar). Select (P=>p.trim ()). Where (p=>p.length>0). ToList ();}
My program is like this:
using(StreamReader sr =NewStreamReader (FilePath)) { stringLine ; while(line = Sr. ReadLine ())! =NULL) { if(i = =int. Parse (TextBox1.Text)-1) {List<string> Dataitems=line. Split (' '). Select (P = = P.trim ()). Where (p = p.length >0). ToList (); DoubleAngleX = (Double. Parse (Dataitems[dataitems.count-1])) * Pi/ the; ArrayList DATAITEMS2=NewArrayList (Dataitems); Dataitems.remove (Dataitems2[dataitems.count-1]);
Another problem is that if the list is String[]dataitems, the operation of an element in the list is not removed, and Dataitems is converted to ArrayList dataItems1. If list<string>dataitems, you can delete an element directly with Dataitems.remove (Dataitems[dataitems.count]).
C # Read TXT file