C# ReadFile _ 方法返回切片为锯齿状数组的字符串
我需要根据条件实现一个方法:
char[][] Foo(StreamReader stream, int arraySize) {}
方法应该返回一个基础字符串,该字符串根据 arraySize 切成锯齿状的字符数组。
第一个条件。如果流是 string.Empty 并且 arraySize 是 10:
return Array.Empty<char[]>();
第二个条件。如果流是“Lorem”并且 arraySize 是 5:
return new char[][]
{
new char[] { 'L', 'o', 'r', 'e', 'm' },
};
第三个条件。如果流是“Lorem”并且 arraySize 是 1:
return new char[][]
{
new char[] { 'L' },
new char[] { 'o' },
new char[] { 'r' },
new char[] { 'e' },
new char[] { 'm' },
};
第四个条件。如果流是“Loremipsumdolorsitamet”并且 arraySize 是 5:
return new char[][]
{
new char[] { 'L', 'o', 'r', 'e', 'm' },
new char[] { 'i', 'p', 's', 'u', 'm' },
new char[] { 'd', 'o', 'l', 'o', 'r' },
new char[] { 's', 'i', 't', 'a', 'm' },
new char[] { 'e', 't' },
};
使用我的解决方案不起作用 第四个条件;为了测试我的函数,我在 Program.cs 旁边创建了 temp.txt 文件,并将上面提到的值(Lorem、Loremipsumdolorsitamet)放在那里。
实现:
char[][] Foo(StreamReader stream, int arraySize)
{
long arrLength = 0;
if (streamReader.BaseStream.Length % arraySize == 0)
{
arrLength = streamReader.BaseStream.Length / arraySize;
}
else
{
arrLength = streamReader.BaseStream.Length / arraySize + 1;
}
char[][] array = new char[arrLength][];
for (int i = 0; i < array.Length; i++)
{
array[i] = new char[arraySize];
for (int j = 0; j < arraySize; j++)
{
array[i][j] = (char)streamReader.Read();
}
}
return array;
}
调用函数并测试结果:
int num = 5;
var myArray = Foo(new StreamReader(@"D:\temp.txt"), num);
for (int i = 0; i < myArray.Length; i++)
{
for (int k = 0; k < myArray[i].Length; k++)
{
Console.Write(myArray[i][k] + "\t");
}
Console.WriteLine();
}
结果是:
L o r e m
i p s u m
d o l o r
s i t a m
e t ▒ ▒ ▒
那么,最后一个数组长度是 5(但应该是 - 2),符号是什么: '▒' ?
请帮忙!
I need to implement a method according to conditions:
char[][] Foo(StreamReader stream, int arraySize) {}
Method should return an underlying string that sliced into jagged array of characters according to arraySize.
First condition. If stream is string.Empty and arraySize is 10:
return Array.Empty<char[]>();
Second condition. If stream is "Lorem" and arraySize is 5:
return new char[][]
{
new char[] { 'L', 'o', 'r', 'e', 'm' },
};
Third condition. If stream is "Lorem" and arraySize is 1:
return new char[][]
{
new char[] { 'L' },
new char[] { 'o' },
new char[] { 'r' },
new char[] { 'e' },
new char[] { 'm' },
};
Forth condition. If stream is "Loremipsumdolorsitamet" and arraySize is 5:
return new char[][]
{
new char[] { 'L', 'o', 'r', 'e', 'm' },
new char[] { 'i', 'p', 's', 'u', 'm' },
new char[] { 'd', 'o', 'l', 'o', 'r' },
new char[] { 's', 'i', 't', 'a', 'm' },
new char[] { 'e', 't' },
};
With my solution doesn't works Forth condition; To test my function I've created temp.txt file alongside Program.cs and put there above mentiond values (Lorem, Loremipsumdolorsitamet).
Implementation:
char[][] Foo(StreamReader stream, int arraySize)
{
long arrLength = 0;
if (streamReader.BaseStream.Length % arraySize == 0)
{
arrLength = streamReader.BaseStream.Length / arraySize;
}
else
{
arrLength = streamReader.BaseStream.Length / arraySize + 1;
}
char[][] array = new char[arrLength][];
for (int i = 0; i < array.Length; i++)
{
array[i] = new char[arraySize];
for (int j = 0; j < arraySize; j++)
{
array[i][j] = (char)streamReader.Read();
}
}
return array;
}
call function and test results:
int num = 5;
var myArray = Foo(new StreamReader(@"D:\temp.txt"), num);
for (int i = 0; i < myArray.Length; i++)
{
for (int k = 0; k < myArray[i].Length; k++)
{
Console.Write(myArray[i][k] + "\t");
}
Console.WriteLine();
}
Resalt is:
L o r e m
i p s u m
d o l o r
s i t a m
e t ▒ ▒ ▒
So, last array length is 5 (but should be - 2) and what the symbol is: '▒' ?
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经修正了我的错误:
I've fixed my error:
如果您使用 .NET6,则可以使用 Chunk 方法将输入拆分为块:
如果这不可能,您可以将当前块的大小计算为
Math.Min(arraySize, array.Length - i * arraySize)
并在for (int i...)
循环中使用它代替arraySize
。 (array.Length - i * arraySize
是剩余长度):If you are on .NET6, you can make use of the Chunk method to split the input into chunks:
If this is not possible, you may calculate the size of the current chunk as
Math.Min(arraySize, array.Length - i * arraySize)
and use this instead ofarraySize
in yourfor (int i...)
loop. (array.Length - i * arraySize
is the remaining length):这可能不是最好的方法,但为了简单起见,您可以创建一个
List
,然后使用StreamReader.ReadBlock()
填充缓冲区。然后,根据该块中读取的字符数(通过
.ReadBlock()
方法方便地以 int 形式返回),实例化一个正确大小的数组并将其添加到列表中,使用最后的.ToArray()
生成您的char[][]
。This might not be the best way, but for simplicity's sake you can just make a
List<char[]>
and then useStreamReader.ReadBlock()
to fill a buffer.Then, based on the number of characters read in that block (conveniently returned as an int by the
.ReadBlock()
method), you instantiate an array of the correct size and add it to the list, using.ToArray()
at the end to produce yourchar[][]
.