取前3位数字并将其保存到另一个文本文件 [英] take the first 3 digits and save it to another text file
本文介绍了取前3位数字并将其保存到另一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文本文件的前3位数字转换为另一个文本文件。我的文件是test.txt
I want to take the first 3 digits of the text file into another text file. My file is test.txt
195^20140212^0^H^000000^A
312^20140211^0^H^010000^A
312^20140211^0^H^020000^A
我只需要在另一个文本文件中取195,312,312。怎么可能?我已经尝试了以下但我无法将其保存到另一个文本文件。
Where I only need to take the 195,312,312 in another text file. How can It be possible? I have tried the below but I am not able to save it to another text file.
string test = @"\\Fdrtse\POI\Debopam\test.txt";
var text = File.ReadAllLines(test);
while (text != null) {
foreach(var line in text) {
string[] dataArray = line.Split('^');
}
}
推荐答案
while(text!= null)
条件没有意义,File.ReadAllLines()
总是返回一个字符串数组(可能除非它抛出异常。
在你分裂()
之后进入dataArray
,您应该将dataArray [0]
值收集到另一个列表< string>
然后在读完所有输入文件后,将该列表的内容写入输出文件,格式化为您的目的。
Thewhile (text != null)
condition is pointless,File.ReadAllLines()
always returns an array of strings (possibly empty) unless it throws an exception.
After youSplit()
the line intodataArray
, you should collect thedataArray[0]
values into anotherList<string>
and then at the completion of reading all of the input file, write the contents of that list to your output file, formatting appropriately for your purpose.
这篇关于取前3位数字并将其保存到另一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文