【发布时间】:2016-09-26 18:43:17
【问题描述】:
我有一个包含 ID、路径和文件名的字符串数组。它们都用分号分隔,看起来像这样:
1.235.554;C:\somewhere\somewhere\this 是 doc.txt 的名称;这是 doc 的名称
我的代码如下:
//string array already has data
string[] file_final;
//gets height to find array size
int height = file_final.GetLength(0);
//declares 2d array
string[,] table = new string[height, 3];
for(int i = 0; i < height; i++){ //loops until height is hit
foreach(char c in file_final[i]) //checks each char in line[i]
{
if(c != ';'){ //if not ; then
string temp; //I would like to save each char into this string
//temp = temp append/insert/+ didnt work
// I know data conversion from char to string is an issue
}
else
{
}
}
}
我想要的输出是:
[0,0] = 1.235.554 (ID)
[0,1] = C:\somewhere\somewhere\this 是 doc.txt 的名称(路径)
[0,2] = 这是文档的名称(文件名)
以此类推,直到我们用完文件中的行来读取。
我在使用 char 到 string 时遇到问题,我知道这是两种不同的数据类型,但我认为 append 会起作用。我是否应该将每个字符保存到临时字符串中的分号,然后将其添加到数组中,清除我的字符串,继续读取该行的其余部分,增加我的数组索引然后再次保存直到行尾?
【问题讨论】:
-
您应该只使用任何库来解析 CSV 并将记录存储在自定义类列表中。 (根本不回答您的问题,但如果您对此感兴趣,实际上可能会帮助您实现目标)。
-
我也会使用jagged arrays。
-
我建议在
C# Stackoverflow Convert CSV into DataTable上进行谷歌搜索你想要并将字符串拆分成一个数组,这个有很多选项..googlestring.Split()函数 -
我只能假设这是一个学习 for 循环或某种字符串处理的项目。否则,为什么要存储任何东西...从文件中读取一行...在';'上拆分行输出分割的结果……阅读下一行……等等。没有理由存储任何东西。如果有存放物品的原因。由 ID、Path 和 FileName 组成的类会是一种更好、更简单的方法。
-
@JohnG 这不是一个学习循环的项目。我需要表格来获取文件并在 aspx 页面上输出结果。
标签: c# arrays string foreach char