我有一个包含ID,路径和文件名的字符串数组。它们都用分号分隔,它看起来像这样:
1.235.554; C:\ somewhere \ somewhere \这是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 \这是doc.txt(Path)的名称
[0,2] =这是doc(文件名)
的名称
依此类推,直到我们用完文件中的行来阅读。
我遇到char到string的问题,我知道这是两种不同的数据类型,但我认为append会起作用。我应该保存每个字符,直到我的临时字符串中的半冒号,然后将其添加到数组,清除我的字符串,继续读取该行的其余部分,增加我的数组索引然后再次保存,直到行结束?
答案 0 :(得分:1)
如果您可以将数据存储在string[][]
而不是string[,]
,
string[][] table = File.ReadAllLines(filePath)
.Select(line => line.Split(';'))
.ToArray();
如果您确实需要将数据存储在多维数组中,可以使用此扩展方法将锯齿状数组转换为二维数组:
public static TSource[,] To2D<TSource>(this TSource[][] jaggedArray)
{
int firstDimension = jaggedArray.Length;
int secondDimension = jaggedArray.GroupBy(row => row.Length).Single().Key;
TSource[,] result = new TSource[firstDimension, secondDimension];
for (int i = 0; i < firstDimension; ++i)
for (int j = 0; j < secondDimension; ++j)
result[i, j] = jaggedArray[i][j];
return result;
}
然后您将拥有以下代码:
string[,] table = File.ReadAllLines(filePath)
.Select(line => line.Split(';'))
.ToArray()
.To2D();
答案 1 :(得分:1)
Hello user5468794因为我没有很好地解释它。让我举一个例子。
首先创建一个ID对象......
class IDObject
{
private string id;
private string path;
private string fName;
//public properties
public string Id
{
get { return id; }
set { id = value; }
}
public string Path
{
get { return path; }
set { path = value; }
}
public string FName
{
get { return fName; }
set { fName = value; }
}
// constructor
public IDObject(string inID, string inPath, string inFName)
{
id = inID;
path = inPath;
fName = inFName;
}
然后在Main
public Form1()
{
InitializeComponent();
// since you have an array of the strings...
List<string> allStrings = getSomeStrings(); // you do not specify how you get these string
List<IDObject> arrayOfAll_IDObjects = new List<IDObject>();
foreach (string curString in allStrings)
{
string[] splitStringArray = curString.Split(';');
IDObject curIDObj = new IDObject(splitStringArray[0], splitStringArray[1], splitStringArray[2]);
arrayOfAll_IDObjects.Add(curIDObj);
}
// now you have a single array of the IDObjects
// you can loop thru it and make your 2 dimensional array if needed
int row = 0;
string[,] twoDimArray = new string[arrayOfAll_IDObjects.Count, 3];
foreach (IDObject curID in arrayOfAll_IDObjects)
{
twoDimArray[row, 0] = curID.Id;
twoDimArray[row, 1] = curID.Path;
twoDimArray[row, 2] = curID.FName;
row++;
}
}
private List<string> getSomeStrings()
{
List<string> allStrings = new List<string>();
allStrings.Add(@"1.235.554;C:\somewhere\somewhere\this is name of doc.txt;this is name of doc");
allStrings.Add(@"2.235.554;C:\somewhere\somewhere\this is name of doc.txt;this is name of doc");
allStrings.Add(@"3.235.554;C:\somewhere\somewhere\this is name of doc.txt;this is name of doc");
allStrings.Add(@"4.235.554;C:\somewhere\somewhere\this is name of doc.txt;this is name of doc");
allStrings.Add(@"5.235.554;C:\somewhere\somewhere\this is name of doc.txt;this is name of doc");
return allStrings;
}
希望这有帮助。