C# 如何读取txt文件?

raymond123456 2010-10-11 05:43:59
txt文件的格式是这样的:
H20101005CJ61 ---这是第一行
D00000130374957867001000110001002201006050020100604832838001001001
D00000230374957903601000110001002201006050020100604832838001001001
D00000330374958126601000110001002201006050020100604832836001001001
D00000430374958019201000110001002201006050020100604832836001001001
T000004 ---这是最后一行


请问我要把 303749578670 303749579036 303749581266 303749580192 截取出来怎么弄?

...全文
157 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
hujinn 2010-10-12
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
raymond123456 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fangxinggood 的回复:]
string path = @"c:\temp\MyTest.txt";
string[] lines = File.ReadAllLines(path);
List<string> data = new List<string>();
for (int i=0; i<lines.Length; i++)
{
if(i > 0 && i < lines.Length ……
[/Quote]

谢谢!
空白画映 2010-10-11
  • 打赏
  • 举报
回复
学习一下,我是一个新手所以我诚心的学习,我积分不多所以我得天天发帖赚积分来提问。
无乐不作111 2010-10-11
  • 打赏
  • 举报
回复
楼上的有用过正则表达式了
感觉应该可以
sloveb520 2010-10-11
  • 打赏
  • 举报
回复
keke453042926 2010-10-11
  • 打赏
  • 举报
回复
使用正则表达式很方便

StreamReader sr = new StreamReader("f:\\a.txt");
string s = sr.ReadToEnd();
string[] str= Regex.Split(s, "\r\n");
for (int i = 1; i < str.Length-1; i++)
{
string ss=Regex.Match(str[i], "D00000"+i.ToString()+"(\\d+)0100011000100220100605002010060(\\d+)001001001").Groups[1].Value;
Console.WriteLine(ss);
}
guliping1991718 2010-10-11
  • 打赏
  • 举报
回复
substring(start,end)
yilaozhuang 2010-10-11
  • 打赏
  • 举报
回复
到百度上google一下,很多例子,很简单
coderQcoder 2010-10-11
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;//添加库

namespace tttttttt
{
class Program
{
static void Main(string[] args)
{
string filepath=@"d:\file.txt";//文件所在
FileStream fl = new FileStream(filepath, FileMode.Open, FileAccess.Read);
StreamReader rl = new StreamReader(fl);
rl.ReadLine();
for (int i = 0; i < 4; i++)
{
string readline = rl.ReadLine();
readline = readline.Substring(7, 12);
Console.WriteLine(readline);
}
rl.Close();
fl.Close();
Console.Read();

}
}
}
wuyq11 2010-10-11
  • 打赏
  • 举报
回复
foreach(string s in File.ReadAllLines(""))
{
//substring 截取 D000002 010001之间数据
}
机器人 2010-10-11
  • 打赏
  • 举报
回复
string path = @"c:\temp\MyTest.txt";
string[] lines = File.ReadAllLines(path);
List<string> data = new List<string>();
for (int i=0; i<lines.Length; i++)
{
if(i > 0 && i < lines.Length - 1)
{
data.Add(lines[i].SubString(7, 12));
}
}
troy2012 2010-10-11
  • 打赏
  • 举报
回复
需要有规则,然后按照规则截取字符串。
缭绕飘渺 2010-10-11
  • 打赏
  • 举报
回复
你这个按行读取
去掉首行和尾行
中间的每行按你那个固定的位置截取字符串就行了
苦苦挣扎中 2010-10-11
  • 打赏
  • 举报
回复
看一点数据也总结不出来规律,你多看些数据,找到规律后就按规律去截取
phil999 2010-10-11
  • 打赏
  • 举报
回复

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧