[英]read data from file with StreamReader into array of line 1
StreamReader text = new StreamReader(@textBox1.Text.ToString());
String tempArray = text.ReadToEnd();
char[] charA = tempArray.ToCharArray();
Console.Write(charA);
output is like that 输出是这样的
****4****
**26*71**
871***694
*6*****4*
2*59*67*8
*8*****2*
658***471
**94*85**
****7****
but ı want to write and save 1d array on 1 row simple ; 但是我想在1行上简单地写和保存1d数组;
****4**** 26*71 871***694 *6*****4* 2*59*67*8 *8*****2* 658***471 94*85 ****7**** **** 4 **** 26 * 71 871 *** 694 * 6 ***** 4 * 2 * 59 * 67 * 8 * 8 ***** 2 * 658 *** 471 94 * 85 **** 7 ****
how can ı split this ? 我该如何拆分?
There's a lot going on in the code you've shared: 您共享的代码中发生了很多事情:
StreamReader text = new StreamReader(@textBox1.Text.ToString());
String tempArray = text.ReadToEnd();
char[] charA = tempArray.ToCharArray();
Console.Write(charA);
textBox1.Text
already is a string
, so you don't need the .ToString()
behind that. textBox1.Text
已经是一个string
,因此您不需要在其后的.ToString()
。
By the way you don't need to prefix a field with a @
unless it's the field has the same name as a keyword. 顺便说一句,除非该字段与关键字具有相同的名称,否则您无需在该字段前面加上
@
。 You're free to do it anyway if you like, though). 不过,如果您愿意,也可以自由地这样做。)
StreamReader text = new StreamReader(textBox1.Text);
String tempArray = text.ReadToEnd();
char[] charA = tempArray.ToCharArray();
Console.Write(charA);
Next, if all you want to do with the file is read all its contents as a string
, you might want to use File.ReadAllText()
as it doesn't keep the file open: 接下来,如果您要对文件进行的所有操作都以
string
形式读取其所有内容,则可能要使用File.ReadAllText()
因为它不会使文件保持打开状态:
String tempArray = File.ReadAllText(textBox1.Text);
char[] charA = tempArray.ToCharArray();
Console.Write(charA);
The console outputs multiple lines but the char[]
still really is a 1D array. 控制台输出多行,但
char[]
仍然实际上是一维数组。 The problem is that there are newline characters in the file, which get processed by the console. 问题在于文件中包含换行符,这些字符由控制台处理。
If you want to remove these newlines, it's easier to do so while you're still working with a string
- because there are quite a few built-in features for this. 如果要删除这些换行符,则在仍使用
string
这样做会更容易-因为此功能有很多内置功能。 Someone already commented using string.Replace("\\r\\n", "")
to do this trick: 有人已经使用
string.Replace("\\r\\n", "")
进行了评论:
String tempArray = File.ReadAllText(textBox1.Text);
tempArray = tempArray.Replace("\r", "");
tempArray = tempArray.Replace("\n", "");
char[] charA = tempArray.ToCharArray();
Console.Write(charA);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.