【发布时间】:2016-12-03 19:09:45
【问题描述】:
在可视化 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.”
我该怎么做?
【问题讨论】:
标签: c# string visual-studio text line