在Visual C#中,我正在执行的按钮需要读取.txt文件,并检查文本文件上的每一行是否以特定字符结尾,如果是,则将该行上的名称打印出来并打印到消息框。到目前为止,我已经设法检查行尾是否存在指定字符的条件,但无法获取名称,因为它位于两组数字之间。该名称紧跟在行上的第一个字符之后,就在一组数字开始之前,因为它们是用户的ID。如何在文本文件的一行中获取特定的句子?
这是到目前为止我的代码内按钮:
private void button1_Click(object sender, EventArgs e)
{
string line, lastchar;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"rato.txt");
while ((line = file.ReadLine()) != null)
{
lastchar= line.Substring(line.Length - 1, 1);
if (lastchar== "2") MessageBox.Show("Prints the name of the user here");
}
file.Close();
}
这是文本文件:
1Paulo111.111.111-11addaqwe2
2Rambo425.433-628-43ererssd3
1Momba111.111.111-11asdsad4432
1Mauricio111.111.111-22wwcssfd2
1Saulo111.111.111-11qwe1231231
使按键需要检查,如果当前行与“2”结束并在行中打印名称。例如,第一行中的名称是Paulo,当它以“2”结尾时,“Paulo”将被打印到消息框,就像第三行和第四行一样。否则,它会跳到下一行。 然后它将被打印在消息框中:“Paulo,Momba,Mauricio。”
我该怎么做?
查找“正则表达式”(https://msdn.microsoft.com/en-us/library/hs600312(v=vs.110).aspx) –