简体   繁体   English

从string []制作二维数组的最佳方法是什么?

[英]What's the best way to make two dimensional array from string[]?

I'm trying to read lines from txt file like this: 我正在尝试从txt文件读取行,如下所示:

string[] data = File.ReadAllLines("data.txt");

What i need to make is char[,] array, which dimensions are: 我需要做的是char [,]数组,其尺寸为:

[single row from txt file chars count] X [row from txt file count].

Then this char array have to be filled with data from string[] data. 然后,必须用来自string []数据的数据填充此char数组。

Is there any easy way to do this ? 有没有简单的方法可以做到这一点? I'm trying to convert string to char somehow but I can't. 我试图以某种方式将字符串转换为char,但我不能。 Any ideas? 有任何想法吗?

You can call ToCharArray on each line. 您可以在每一行上调用ToCharArray

String filepath = @"C:\test.txt";
Char[][] result = File.ReadAllLines(filepath).Select(obj => obj.ToCharArray()).ToArray();

It's worth noting that this will give you a Char[][] (which is a jagged array ), and not Char[,] (which is a multidimensional array ) 值得注意的是,这将为您提供Char[][] (是一个锯齿状数组 ),而不是 Char[,] (这是一个多维数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM