3

我有一个以下字符串,在文本文件中有换行符:

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);包含我想要的单独的单独单词。但是,我被困在如何转换为多维数组并将其传递给数据库表。

4

2 回答 2

7

好的 'ol 老式 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 个元素和后 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'
于 2013-06-07T04:08:39.947 回答
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;
}
于 2013-06-07T04:19:29.880 回答