我有一个跟随字符串,在文本文件中有换行符:
0100000184998 1 2500855884500 /RAM PRADHAN 1302 Cold
0100000186936 1 2515407774500 /HARI SHRESTHA 1302 Cold
0100000238562 1 2500211214500 /SARAL SHARMA 1301 Cold
0100000270755 0 1820823730100 /EXPRESS ACCOUNT 9999 Cold
0100000272629 0 1820833290100 /LIZA MAHARJAN 1208 Cold
0100000272637 0 2510171014500 /MAYA SHRESTHA 1208 Cold
0100000272645 0 1800505550100 /DR RIMA BHATTA 1208 Cold
0100000272652 0 1820834930100 /KAMAL BALAMI 1208 Cold
0100000272678 0 1810024300100 /VINOD BHAKTA POKHAREL 1208 Cold
我想将该字符串放在数组中,并且对于每个新行,跳转数组中的一个索引位置。然后,将数据存储到数据库表中。 所以,数组就像这样
array[0][0] = "0100000184998"
array[0][1] = "1"
array[0][2] = "2500855884500"
array[1][0] = "0100000186936"
array[1][1] = "1"
array[1][3] = "RAM PRADHAN"
我完成了选择文件并从文件部分读取文本。
for (int i = 1; i < contents.Length; i++)
{
// string[] rowData = sourceData[i].Split(' ');
string[] words = contents.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
在这里,Console.WriteLine(word);
包含我想要的单独单词。但是,我被困在如何转换为多维数组并将其传递给数据库表。
答案 0 :(得分:7)
良好的时尚LINQ作品:
@"0100000184998 1 2500855884500 /NITA PRADHAN 1302 Cold
0100000186936 1 2515407774500 /SARAL SHRESTHA 1302 Cold
0100000238562 1 2500211214500 /RASHMI SHARMA 1301 Cold
0100000270755 0 1820823730100 /EXPRESS ACCOUNT 9999 Cold
0100000272629 0 1820833290100 /SUNIL MAHARJAN 1208 Cold
0100000272637 0 2510171014500 /AJANTA SHRESTHA 1208 Cold
0100000272645 0 1800505550100 /DR BINOD BHATTA 1208 Cold
0100000272652 0 1820834930100 /PALAN BALAMI 1208 Cold
0100000272678 0 1810024300100 /BHANU BHAKTA POKHAREL 1208 Cold"
.Split(new char[]{ '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select (x => {
string[] parts = x.Split(new char[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries);
return parts.Take(3)
.Union(new[]{ String.Join(" ", parts.Skip(3).Take(parts.Length - 3 - 2)) })
.Union(parts.Skip(parts.Length - 2)).ToArray();
}).ToArray();
这将抓住前3个元素&amp;最后2个元素,然后采取中间的任何内容并创建另一个元素。结果(前4个结果
result[0][0] = '0100000184998'
result[0][1] = '1'
result[0][2] = '2500855884500'
result[0][3] = '/NITA PRADHAN'
result[0][4] = '1302'
result[0][5] = 'Cold'
result[1][0] = '0100000186936'
result[1][1] = '1'
result[1][2] = '2515407774500'
result[1][3] = '/SARAL SHRESTHA'
result[1][4] = '1302'
result[1][5] = 'Cold'
result[2][0] = '0100000238562'
result[2][1] = '1'
result[2][2] = '2500211214500'
result[2][3] = '/RASHMI SHARMA'
result[2][4] = '1301'
result[2][5] = 'Cold'
result[3][0] = '0100000270755'
result[3][1] = '0'
result[3][2] = '1820823730100'
result[3][3] = '/EXPRESS ACCOUNT'
result[3][4] = '9999'
result[3][5] = 'Cold'
result[4][0] = '0100000272629'
result[4][1] = '0'
result[4][2] = '1820833290100'
result[4][3] = '/SUNIL MAHARJAN'
result[4][4] = '1208'
result[4][5] = 'Cold'
result[5][0] = '0100000272637'
result[5][1] = '0'
result[5][2] = '2510171014500'
result[5][3] = '/AJANTA SHRESTHA'
result[5][4] = '1208'
result[5][5] = 'Cold'
result[6][0] = '0100000272645'
result[6][1] = '0'
result[6][2] = '1800505550100'
result[6][3] = '/DR BINOD BHATTA'
result[6][4] = '1208'
result[6][5] = 'Cold'
result[7][0] = '0100000272652'
result[7][1] = '0'
result[7][2] = '1820834930100'
result[7][3] = '/PALAN BALAMI'
result[7][4] = '1208'
result[7][5] = 'Cold'
result[8][0] = '0100000272678'
result[8][1] = '0'
result[8][2] = '1810024300100'
result[8][3] = '/BHANU BHAKTA POKHAREL'
result[8][4] = '1208'
result[8][5] = 'Cold'
答案 1 :(得分:0)
您可以使用以下功能!!
using System;
using System.IO;
using System.Collections.Generic;
List<string[]> some_function (string fn) { //fn = "path/to/file"
string line;
List<string[]> rval = new List<string[]>();
if (File.Exists(fn)) {
StreamReader file = new StreamReader(fn);
while ((line = file.ReadLine()) != null) {
string[] value = line.Split(' ');
rval.Add(value);
}
file.Close();
}
return rval;
}