Home > Net >  C # read TXT data line by line, and stored in a two-dimensional array
C # read TXT data line by line, and stored in a two-dimensional array

Time:09-20

I have a TXT documents, data format is roughly as follows:
ID: 1; Value: 10; ID: 2; Value: 20; ID: 3; Value: 30; ID: 4; Value: 40;
.

Want to achieve by extracting the result value [0, 0]="1" value [0, 1]="10" like this

CodePudding user response:

What this logic, the value [0, 0] until the value (0, n]?
Why not use a dictionary Value] [ID,

CodePudding user response:

 
Public class Values
{
Public int id;
Public int value.
Public Values (int, int v) {id=I; Value=https://bbs.csdn.net/topics/v; }
}
Public List GetValues=new List (a);

Private void button1_Click (object sender, EventArgs e)
{
String read_text="ID: 1; Value: 10; ID: 2; Value: 20; ID: 3; Value: 30; ID: 4; Value: 40; ";//read the text of the
String [] arr=read_text. Replace (" ", ""). The Split (new string [] {";" , ":", "ID", "Value"}, StringSplitOptions. RemoveEmptyEntries);
for (int i=0; i{
GetValues. Add (new Values (Convert ToInt32 (arr) [I], the Convert. ToInt32 (arr) [I + 1]));
}
}

CodePudding user response:

Your title says read line by line, a line is a group ID: 1; Value: 10? Then use a dictionary as a two-dimensional array? Below is read TXT, then assigned to the dictionary code,
 
StreamReader sr=new StreamReader (path, Encoding. The Default).
String line;
Dictionary Dic=new Dictionary (a);
While ((line=sr. ReadLine ())!=null)
{
The string key=line. The Split ('; ') [0]. Replace (" ID ", ""). The Trim ();
The string value=https://bbs.csdn.net/topics/line.Split ('; ') [1]. The Replace (" Value ", ""). The Trim ();
Dic. The Add (int. Parse (key), int. J Parse (value));
}

CodePudding user response:

Why don't you look at this:
First you read line by line, then according to; Splite, according to: splite
Var STR="ID: 1; Value: 10; ID: 2; Value: 20; ID: 3; Value: 30; ID: 4; Value: 40;" Splite ('; ');
Int [] []=arr=new [rows] [];
for(int i=0; i//the first
Arr [0] [0]=STR [I] splite (' : ') [1];
}
  •  Tags:  
  • C#
  • Related