How to find and store specific lines from a file in c# -
i want search lines containing specific data , store these data in list, how can this? have written code project, code can find , store first line containing wanted data because these lines started same structure, example, data repeated in lines 100, 250, 400, 660, , these lines stated "|prod |oil |" .
double[] oil_prdc = new double[10]; double[] water_prdc = new double[10]; double[] gas_prdc = new double[10]; double[] water_injc = new double[10]; double[] gas_injc = new double[10]; int length_time = 5; string[][] characters = new string[2391][]; string [] charss; string[][]counter = new string[20][]; while ((line = file.readline()) != null) { (int = 0; < length_time; i++) { total_production.add(null); total_production[i] = new list<_5_points>(); while (line.contains("|prod |oil |")) { charss = line.split('|'); (int j = 0; j < charss.length; j++) { if (j == 9) { oil_prdc[i] = 1000 * convert.todouble(charss[j]); } else if (j == 10) { water_prdc[i] = convert.todouble(charss[j]); } else if (j == 11) { gas_prdc[i] = 1000 * convert.todouble(charss[j]); } } count++; if (count > charss.length) break; } while (line.contains(" |winj |wat ")) { charss = line.split('|'); (int jj = 0; jj < charss.length; jj++) { if (jj == 8) { water_injc[i] = 1000 * convert.todouble(charss[jj]); } } count++; if (count > charss.length) break; } while (line.contains(" |ginj |passive ")) { charss = line.split('|'); (int ij = 0; ij < charss.length; ij++) { if (ij == 9) { gas_injc[i] = 1000 * convert.todouble(charss[ij]); } } count++; if (count > charss.length) break; } _5_points temp=new _5_points{oil_prd =oil_prdc[i],gas_prd=gas_prdc[i],water_prd=water_prdc[i],water_inj=water_injc[i],gas_inj=gas_injc[i]}; total_production[i].add(temp); } }
example of how read file:
try { // create instance of streamreader read file. // using statement closes streamreader. using (streamreader sr = new streamreader("testfile.txt")) { string line; // read , display lines file until end of // file reached. while ((line = sr.readline()) != null) { console.writeline(line); } } } catch (exception e) { // let user know went wrong. console.writeline("the file not read:"); console.writeline(e.message); }
search specific text in each "line":
public static string getbetween(string strsource, string strstart, string strend) { int start, end; if (strsource.contains(strstart) && strsource.contains(strend)) { start = strsource.indexof(strstart, 0) + strstart.length; end = strsource.indexof(strend, start); return strsource.substring(start, end - start); } else { return ""; } }
how use it:
string text = "this example string , data here"; string data = getbetween(text, "my", "is");
Comments
Post a Comment