[英]How to split a text file without delimiters
我有一个涉及文件 IO 和二维数组的家庭作业。 我们得到了一个如下所示的文本文件:
10,10
@,-
oxxxxxxxxo
xoxxxxxxox
xxoxxxxoxx
xxxoxxoxxx
xxxxooxxxx
xxxxooxxxx
xxxoxxoxxx
xxoxxxxoxx
xoxxxxxxox
oxxxxxxxxo
我能够正确加载文本文件,前两行不是问题,但我试图将每一行分成单独的单个字符,以便我可以将它们放入 2D 数组中。 我不能使用正则表达式,并且花了几个小时试图找到答案。 有谁知道如何做到这一点? 谢谢。
using System;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string value = @"10,10
@,-
oxxxxxxxxo
xoxxxxxxox
xxoxxxxoxx
xxxoxxoxxx
xxxxooxxxx
xxxxooxxxx
xxxoxxoxxx
xxoxxxxoxx
xoxxxxxxox
oxxxxxxxxo";
string[] allLines = value.Split(new string[1] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
string firstLine = allLines[0]; // 10,10
string secondLine = allLines[1]; // @,-
string[] rest = Enumerable.Take<string>(allLines, allLines.Length - 2).ToArray<string>();
// Inconvenient : as we use jagged/multidimensional arrays here, the length must be known in advanceand must be the same in each line we read.
char[,] valuesAs2DArray = new char[rest.Length,10];
int i = 0;
foreach (string s in rest.Skip(2))
{
for (int j = 0; j < s.Length;j++)
{
valuesAs2DArray[i,j] = s[j];
}
i++;
}
Console.ReadKey();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.