如何从C#windowsforms中的文本文件中获取特定行的最后一个单词 [英] How to get the specific line last word from text file in C# windowsforms
问题描述
我有1000行的文本文件,我有一个日期,每个行使用日期我想获取该行,并应显示datagridview窗体中的最后一个单词。
例如:-my text like
2017/07/22 4:50记录插入--------- 20
2017/07/20 5:55记录插入--------- 15
2017/07/23 2:30记录插入------- - 29
当我输入想要显示当天记录的日期
< b>我尝试了什么:
I have text file with 1000 of lines and i have a date for every line by using date i want to fetch that line and should display the last words in datagridview windows forms.
For ex:-my text like
2017/07/22 4:50 records inserted --------- 20
2017/07/20 5:55 records inserted --------- 15
2017/07/23 2:30 records inserted --------- 29
when i enter a date it want to display the record for that day
What I have tried:
when i enter a date it want to display the record for that day
推荐答案
string text = "2017/07/22 4:50 records inserted --------- 20\n2017 / 07 / 20 5:55 records inserted ---------15\r\n2017 / 07 / 23 2:30 records inserted ---------29\r";
var lines = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
>
你可以使用下面的代码逐行放射文本文件并写入你的逻辑来读取最后一个单词。
You can rad a text file line-by-line using below code and writ your logic to read last word.
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
string lastWord = line.Split(' ').Last();
}
file.Close();
如果你没有LINQ或者做不想用,下面是代码..
If you do not have LINQ or do not want to use, below is the code..
string[] parts = line.Split(' ');
string lastWord = parts[parts.Length - 1];
这篇关于如何从C#windowsforms中的文本文件中获取特定行的最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!