如何读取文件中的特定单词 [英] how to read particular word in file
本文介绍了如何读取文件中的特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从文本文件中读取特定的单词。例如 AAA.txt
是我的文本文件,它包含如下所示的单词
Hi,
I want to read a particular word from a text file. For example AAA.txt
is my text file and it contains words as shown below
Name := Dell
Server := Dataserver
User := Mike
我想要的数据是Dataserver。我怎么得到它?
The data i want is Dataserver. How do I get it?
推荐答案
会帮助你
string strvalue = string.Empty;
using(System.IO.StringReader sr = new StringReader(c:\ aaa.txt))
{
string [] strdata = sr.ReadToEnd()。Split(new char [] {'\ n'});
foreach(strdata中的字符串项)
{
if(item.Contains(Server:=))
{
strvalue = item.Replace(Server:=,) ;
休息;
}
}
will Help you
string strvalue = string.Empty;
using (System.IO.StringReader sr = new StringReader("c:\aaa.txt"))
{
string[] strdata = sr.ReadToEnd().Split(new char[] { '\n' });
foreach (string item in strdata)
{
if (item.Contains("Server :="))
{
strvalue = item.Replace("Server :=","");
Break;
}
}
你们真的用了
Key:= Value
或更确切地说
key = value
?在最后一种情况下,您可以将Windows API用于ini文件。
否则,您必须逐行读取文件并使用正则表达式解析每一行。
Do you really use
Key :=Value
or rather
key=value
? In the last case, you can use the Windows API for ini-files.
Otherwise, you have to read the file line by line and parse each line with a regex.
使用下面的代码获取file.txt中的指定字符串
之后你删除字符串=;
Use the below code to get the specified string in the file.txt
After that you remove the strings upto "=";
string regexMatchString= "Server :=Dataserver";
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\Textfile.txt");
string matchedString = Regex.Match(text, regexMatchStrig).ToString();
这篇关于如何读取文件中的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文