在文本文件中搜索单词 [英] Search words in text file
本文介绍了在文本文件中搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI,
我有文本文件,其中包含以下文本.
I have text file, it contain following text.
Starting Ping at
The current time is: 18:26:15.37
Enter the new time:
Pinging 192.168.1.143 with 32 bytes of data:
Reply from 192.168.1.143: bytes=32 time=12ms TTL=123
Reply from 192.168.1.143: bytes=32 time=12ms TTL=123
Reply from 192.168.1.143: bytes=32 time=12ms TTL=123
Reply from 192.168.1.143: bytes=32 time=12ms TTL=123
Ping statistics for 192.168.1.143:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 12ms, Maximum = 12ms, Average = 12ms
在这里,我需要从文本文件中提取相位.
例如,我需要节省时间和最小值.
像这样18:26:15.37 12ms
在c#
And here I need to extract the phase from the text file.
for an example I need to save time and the Minimum value.
like this 18:26:15.37 12ms
Is this possible to do in c#
推荐答案
中可以做到吗?
var finalString = string.Empty;
var line = string.Empty;
var file = new System.IO.StreamReader("c:\\text.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("The current time is:"))
{
var strrr = Regex.Split(line, "The current time is: ");
finalString = strrr[1];
}
else if (line.Contains("Minimum ="))
{
var strrr = Regex.Split(line, "Minimum =");
finalString += strrr[1].Substring(0,5);
break;
}
}
file.Close();
Console.WriteLine(finalString);
Console.ReadLine();
您可以使用名为Contains,Substring和IndexOf的字符串类中的方法.您可以定义性地全部使用它们.
希望对您有所帮助.
You can use methods from the string class called Contains, Substring & IndexOf. Definetively you can use them all.
Hope it helps.
这篇关于在文本文件中搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文