c# 如何读取txt特定字符后面的数字并复制到另一txt?
我有一堆数字:第一行:1,2,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,...
我有一堆数字:第一行:1,2,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,0,0,第二行:3,4,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,0,0,第n行:。。。。。。。。。。。。。我要做的就是读取(数字.数字,数字.数字,数字.数字,数字.数字,),只要符号(.,.,)之间的数字,从开头开始搜,遇到 . 判断后面是不是(,. ,),若是这串都考,不是继续往下读,直到第一行结束,开始读第二行。。。到第n行,最后将符合条件的值拷进新的txt~求大神们帮帮忙!
展开
3个回答
展开全部
请楼主看下我的理解,是否正确,我的D盘下面有一个11.txt的文件,内容如下:
1,2,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,0,0
3,4,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,0,0
1,2,113.2345,abc,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,0,0
请注意,第三条的第四个数有一个aaa。。说明这条数据是不满足要求。
List<string> result = new List<string>();
FileStream fs = new FileStream(@"D:\11.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
string[] allNumbers = str.Split(',');
bool isResult = true;
foreach (string s in allNumbers)
{
double i;
bool flag = double.TryParse(s, out i);
if (!flag)
isResult = false;
}
if (isResult)
result.Add(str);
}
foreach (string str in result)
{
listBox1.Items.Add(str);
}
得到的结果是前两条数据:
追问
不好意思 没描述清楚,我的意思是只要每行的113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,这串,就是符号(.,.,)之前的数字,如果一行里遇到不满足这样的就跳过继续看,知道一行结束,再下一行
展开全部
使用,. , 作为分隔符拆分成数组
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没看懂你到底要什么数字,第一行你举个例子
更多追问追答
追问
第一行要这些:113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,就是(.,.,)之间的数字
追答
using System.Text.RegularExpressions;
using System.Collections;
//要处理的字符串
string[] lines = {"1,2,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,0,0,",
"3,4,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,0,0,0,",};
List<string> resultList = new List<string>();
string pattern = @"\d+\.\d+,\d+\.\d+,"; //
Regex reg = new Regex(pattern);
foreach (string str in lines)
{
MatchCollection mc = reg.Matches(str);
ArrayList al = new ArrayList(mc);
resultList.Add(string.Join("", al.ToArray()));
}
// 保存
System.IO.File.WriteAllLines("d:\\123.txt", resultList);
结果:
113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,
113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,113.2345,27.1834,
楼主的表述能力实在让人汗颜
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询