【问题标题】:String array run through string until char then save to 2D array字符串数组遍历字符串直到 char 然后保存到二维数组
【发布时间】: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


【解决方案1】:

如果您可以将数据存储在string[][] 而不是string[,]

使用System.Linq

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();

【讨论】:

    【解决方案2】:

    你好 user5468794 由于我没有很好地解释它..让我举个例子。

    首先创建一个ID Objects类...

    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;
      }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 2011-06-22
      • 2021-06-12
      • 1970-01-01
      • 2019-09-14
      • 2020-09-02
      • 2020-09-15
      • 1970-01-01
      • 2013-08-07
      相关资源
      最近更新 更多