Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串数组运行字符串直到字符,然后保存到2D数组_C#_Arrays_String_Foreach_Char - Fatal编程技术网

C# 字符串数组运行字符串直到字符,然后保存到2D数组

C# 字符串数组运行字符串直到字符,然后保存到2D数组,c#,arrays,string,foreach,char,C#,Arrays,String,Foreach,Char,我有一个包含ID、路径和文件名的字符串数组。它们都被分号分开,看起来像这样: 1.235.554;C:\somewhere\somewhere\这是doc.txt的名称;这是医生的名字 我的代码如下: //string array already has data string[] file_final; //gets height to find array size int height = file_final.GetLength(0); //declares 2d array str

我有一个包含ID、路径和文件名的字符串数组。它们都被分号分开,看起来像这样:

1.235.554;C:\somewhere\somewhere\这是doc.txt的名称;这是医生的名字

我的代码如下:

//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
                {
                }
            }

     }
//字符串数组已经有数据
字符串[]文件\u final;
//获取查找数组大小的高度
int height=file_final.GetLength(0);
//声明二维数组
字符串[,]表=新字符串[高度,3];
for(int i=0;i
我期望的结果是:

[0,0]=1.235.554(ID)

[0,1]=C:\somewhere\somewhere\这是doc.txt(路径)的名称

[0,2]=这是文档的名称(文件名)

以此类推,直到文件中要读取的行用完为止


我对char-to-string有问题,我知道这是两种不同的数据类型,但我认为append可以工作。我是否应该将每个字符保存到临时字符串中的分号,然后将其添加到数组中,清除我的字符串,继续读取行的其余部分,增加我的数组索引,然后再次保存到行尾

如果您同意将数据存储在
字符串[]【】
而不是
字符串[],]

使用


如果确实需要将数据存储在多维数组中,可以使用此扩展方法将锯齿状数组转换为二维数组:

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

如果您愿意将数据存储在
字符串[][]
而不是
字符串[,]

使用


如果确实需要将数据存储在多维数组中,可以使用此扩展方法将锯齿状数组转换为二维数组:

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

您好,用户5468794,因为我没有很好地解释它。。让我举个例子

首先创建一个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;
  }
那么大体上

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;
  }
public Form1()
{
初始化组件();
//因为您有一个字符串数组。。。
List allStrings=getSomeStrings();//不指定获取这些字符串的方式
List arrayOfAll_IDObjects=新列表();
foreach(所有字符串中的字符串curString)
{
string[]splitStringArray=curString.Split(“;”);
IDObject curIDObj=新的IDObject(splitStringArray[0],splitStringArray[1],splitStringArray[2]);
arrayOfAll_IDObjects.Add(curIDObj);
}
//现在您有了一个IDObject数组
//如果需要的话,您可以循环通过它并制作二维数组
int行=0;
string[,]twoDimArray=新字符串[arrayOfAll_IDObject.Count,3];
foreach(阵列中的IDObject curID\u IDObject)
{
twoDimArray[row,0]=curID.Id;
twoDimArray[row,1]=curID.Path;
twoDimArray[row,2]=curID.FName;
行++;
}
}
私有列表GetSomeString()
{
List allStrings=new List();
allStrings.Add(@“1.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“2.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“3.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“4.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“5.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
返回所有字符串;
}

希望这能有所帮助。

你好,用户5468794,因为我没有很好地解释它。。让我举个例子

首先创建一个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;
  }
那么大体上

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;
  }
public Form1()
{
初始化组件();
//因为您有一个字符串数组。。。
List allStrings=getSomeStrings();//不指定获取这些字符串的方式
List arrayOfAll_IDObjects=新列表();
foreach(所有字符串中的字符串curString)
{
string[]splitStringArray=curString.Split(“;”);
IDObject curIDObj=新的IDObject(splitStringArray[0],splitStringArray[1],splitStringArray[2]);
arrayOfAll_IDObjects.Add(curIDObj);
}
//现在您有了一个IDObject数组
//如果需要的话,您可以循环通过它并制作二维数组
int行=0;
string[,]twoDimArray=新字符串[arrayOfAll_IDObject.Count,3];
foreach(阵列中的IDObject curID\u IDObject)
{
twoDimArray[row,0]=curID.Id;
twoDimArray[row,1]=curID.Path;
twoDimArray[row,2]=curID.FName;
行++;
}
}
私有列表GetSomeString()
{
List allStrings=new List();
allStrings.Add(@“1.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“2.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“3.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“4.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
allStrings.Add(@“5.235.554;C:\somewhere\somewhere\this name of doc.txt;this name of doc”);
返回所有字符串;
}

希望这有帮助。

您应该使用任何库来解析CSV,并将记录存储在自定义类列表中。(完全不回答您的问题,但如果您感兴趣的话,可能会帮助您实现目标)。我也会使用。我建议您在
C#Stackoverflow上进行谷歌搜索,将CSV转换为DataTable
,我还创建并发布了几种简单的方法来实现这一点。。您还可以创建一个