文件字符串操作 [英] File String manipulation

查看:93
本文介绍了文件字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有此功能,可以很好地查找和替换myText.txt中的字符串

FileReplaceString("c:\ myText.txt","oldWord","newWord",false);

公共无效FileReplaceString(字符串路径,字符串查找,字符串替换,布尔型匹配项)
{

TextReader r =新的StreamReader(path);
字符串文本= r.ReadToEnd();
r.Close();
文本= Regex.Replace(文本,Regex.Escape(查找),替换,(匹配情况)?RegexOptions.None:RegexOptions.IgnoreCase);
TextWriter w =新的StreamWriter(path);
//MessageBox.Show(find);
w.Write(text);
w.Flush();
w.Close();
}
我不想将"oldword"替换为"newWord".我需要做的是找到字符串"oldword",然后读取下一个5个字符(例如,字符串可能看起来像
xsasdsdadad oldWord12345),因此读取12345
非常感谢您的帮助.

I have this function that works great finding and replacing a string in a myText.txt

FileReplaceString("c:\myText.txt", "oldWord", "newWord", false);

public void FileReplaceString(string path, string find, string replace, bool matchcase)
{

TextReader r = new StreamReader(path);
string text = r.ReadToEnd();
r.Close();
text = Regex.Replace(text, Regex.Escape(find), replace, (matchcase) ? RegexOptions.None : RegexOptions.IgnoreCase);
TextWriter w = new StreamWriter(path);
//MessageBox.Show(find);
w.Write(text);
w.Flush();
w.Close();
}
I do not want to replace "oldword" by "newWord". What I need to do is to find the string "oldword" and then read the next 5 character (i.g. the string may look like
xsasdsdadad oldWord12345) and thus reading 12345
Any help is greatly appreciated.

推荐答案

text.Substring(text.IndexOf("text") + 4, 5);


这有帮助吗?


does that help any?


这篇关于文件字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆