C#查找文本文件里的字符串,找到后,找到后打印出这个字符串末尾开始的后十个字符来,怎样实现?

lcmlhs_2005 2010-09-18 11:24:36
如题:请高手指点
...全文
275 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcmlhs_2005 2010-09-19
  • 打赏
  • 举报
回复
楼上的领会错了,我是说找到字符串后,在该字符串后面截取十个字符的。比如:要找的是字符串是“abc”,如果在文本文件里找到了“abc”字符串,那么就在它后面,即c后面的第一个字符开始,截取10个字符的!!!
bourbon1795 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lcmlhs_2005 的回复:]
引用 3 楼 liulangdeyuyu 的回复:
string str="目标串";
string txt=文本;

string reslute=txt.substring(txt.insideof(str),10);
console.write(reslute);

str的内容怎样从文本里得来呢,要知道我的文本几十兆大小的
[/Quote]
据我所知,一个文本文件最大也就 3M多,
你怎么有几十M呢?
lcmlhs_2005 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liulangdeyuyu 的回复:]
string str="目标串";
string txt=文本;

string reslute=txt.substring(txt.insideof(str),10);
console.write(reslute);
[/Quote]
str的内容怎样从文本里得来呢,要知道我的文本几十兆大小的
xie_xingan 2010-09-18
  • 打赏
  • 举报
回复
用正则表达式匹配,然后用SubString
var rg = new Regex("你的Pattern");
var match = rg.Match("你的文本");
var matchText = match.Group[0].Value; //你找到的字符串
var lastTen = matchText.Substring(matchText.Length - 10);
结束。
lcmlhs_2005 2010-09-18
  • 打赏
  • 举报
回复
我读取的是文本内容啊
lcmlhs_2005 2010-09-18
  • 打赏
  • 举报
回复
我现在用正则表达式找到了位置,可是后面怎么取呢?
liulangdeyuyu 2010-09-18
  • 打赏
  • 举报
回复
string str="目标串";
string txt=文本;

string reslute=txt.substring(txt.insideof(str),10);
console.write(reslute);
josxhn 2010-09-18
  • 打赏
  • 举报
回复
恩,我也想知道,顶一下
ztaimm 2010-09-18
  • 打赏
  • 举报
回复
帮顶!
兔子-顾问 2010-09-18
  • 打赏
  • 举报
回复
FileStream fs = File.OpenRead("你的文件");
if (fs.Length < 10)
{
fs.Close();
return;
}
fs.Seek(-10, SeekOrigin.End);
byte[] buf = new byte[10];
fs.Read(buf, 0, 10);
//Encoding.GetEncoding("gb2312")根据你实际的修改
string result = Encoding.GetEncoding("gb2312").GetString(buf);//result就是你要的
fs.Close();
wsncz 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 liulangdeyuyu 的回复:]
Regex reg=new Regex("查找的条件(?<str>.{10})");
MatchCollection mac=reg.matches(文本);
foreach(match m in mac)
{
console.write(m.groups["str"].value);


}
[/Quote]
showtime5156 2010-09-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lcmlhs_2005 的回复:]

文本文件最大才3M,开玩笑吧,我的上几十兆了啊
[/Quote]
哇,不是开玩笑吧,那要有多少字啊?????????、
lcmlhs_2005 2010-09-18
  • 打赏
  • 举报
回复
楼上的第一句写得正确吗?
liulangdeyuyu 2010-09-18
  • 打赏
  • 举报
回复
Regex reg=new Regex("查找的条件(?<str>.{10})");
MatchCollection mac=reg.matches(文本);
foreach(match m in mac)
{
console.write(m.groups["str"].value);


}
lcmlhs_2005 2010-09-18
  • 打赏
  • 举报
回复
文本文件最大才3M,开玩笑吧,我的上几十兆了啊

111,129

社区成员

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

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

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