如何将字符串放入数组中,在c#中按新行分割
本文关键字:分割 新行 数组 字符串 | 更新日期: 2023-09-27 18:17:16
我有一个下面的字符串,在一个文本文件中换行:
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);
包含我想要的单独的单独的单词。但是,我被困在如何转换成多维数组,并将其传递到数据库表。
经典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'
可以使用下面的函数!!
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;
}