C# 我想在文本文件中获得一个特定的行以显示在文本框中
我有一个.txt文件,其中包含多个相似项的数据,例如:C# 我想在文本文件中获得一个特定的行以显示在文本框中,c#,C#,我有一个.txt文件,其中包含多个相似项的数据,例如: Line 1 has - Name: Tom Line 2 has - Score 1: 10 Line 3 has - Score 2: 5 Line 4 has - Score 3: 5 Line 5 has - Score 4: 7 Line 6 has - Score 5: 10 Line 7 has - Total Score: 37 Line 8 has - Name: Ben Line 9 has - Scor
Line 1 has - Name: Tom
Line 2 has - Score 1: 10
Line 3 has - Score 2: 5
Line 4 has - Score 3: 5
Line 5 has - Score 4: 7
Line 6 has - Score 5: 10
Line 7 has - Total Score: 37
Line 8 has - Name: Ben
Line 9 has - Score 1: 5
Line 10 has - Score 2: 10
Line 11 has - Score 3: 10
Line 12 has - Score 4: 0
Line 13 has - Score 5: 5
Line 14 has - Total Score: 30
我试图做的是从.txt文件中选择行1、(名称:Tom
)和行7(总分:37
)和行8和行14。有没有办法在.txt文件中选择这些特定行,以便在文本框中显示它们
我想要的结果是能够将所选的行放入文本框中。任何帮助/建议都将不胜感激。我将创建一个类来存储名称和分数。这允许您用分数信息填写列表
class Player
{
public string Name { get; set; }
public int Score { get; set; }
public override string ToString()
{
return $"{Name,-15}: {Score,4}";
}
}
您可以枚举行并查看行是否以特定字符串开头
const string path = @"C:\Users\Me\Documents\TheFile.txt";
var players = new List<Player>();
Player currentPlayer = null;
foreach (string line in File.ReadLines(path)) {
if (line.StartsWith("Name:")) {
// We have a new person. Create a `Player` object and add it to the list.
currentPlayer = new Player { Name = line.Substring(6) };
players.Add(currentPlayer);
} else if (line.StartsWith("Total Score:") &&
Int32.TryParse(line.Substring(13), out int score)) {
// We have a score number. Convert it to `int` and assign it to the current player.
currentPlayer.Score = score;
}
}
将数据解析为类。您可以获得姓名和分数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string line = "";
Player newPlayer = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
string[] splitLine = line.Split(new char[] { ':' }).ToArray();
if (line.StartsWith("Name"))
{
newPlayer = new Player();
Player.players.Add(newPlayer);
newPlayer.name = splitLine[1].Trim();
}
else
{
if (line.StartsWith("Score"))
{
if (newPlayer.scores == null) newPlayer.scores = new List<int>();
newPlayer.scores.Add(int.Parse(splitLine[1]));
}
}
}
}
string[] playersNames = Player.players.Select(x => x.name).ToArray();
Console.WriteLine(string.Join(" , ", playersNames));
Console.ReadLine();
}
}
public class Player
{
public static List<Player> players = new List<Player>();
public string name { get; set; }
public List<int> scores { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.txt”;
静态void Main(字符串[]参数)
{
StreamReader=新的StreamReader(文件名);
字符串行=”;
Player newPlayer=null;
而((line=reader.ReadLine())!=null)
{
line=line.Trim();
如果(直线长度>0)
{
string[]splitLine=line.Split(新字符[]{':'}).ToArray();
如果(第行开始时使用(“名称”))
{
newPlayer=新玩家();
Player.players.Add(newPlayer);
newPlayer.name=splitLine[1].Trim();
}
其他的
{
如果(第行开始,以“分数”))
{
如果(newPlayer.scores==null)newPlayer.scores=newlist();
newPlayer.scores.Add(int.Parse(splitLine[1]);
}
}
}
}
string[]playersNames=Player.players.Select(x=>x.name.ToArray();
Console.WriteLine(string.Join(“,”,playersNames));
Console.ReadLine();
}
}
公开课选手
{
public static List players=new List();
公共字符串名称{get;set;}
公共列表分数{get;set;}
}
}
就我个人而言,我只需使用StreamReader进行循环,检查当前正在读取的行是否包含“Name”或“Total”,并将它们存储到流的末尾。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
StreamReader reader = new StreamReader(FILENAME);
string line = "";
Player newPlayer = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
string[] splitLine = line.Split(new char[] { ':' }).ToArray();
if (line.StartsWith("Name"))
{
newPlayer = new Player();
Player.players.Add(newPlayer);
newPlayer.name = splitLine[1].Trim();
}
else
{
if (line.StartsWith("Score"))
{
if (newPlayer.scores == null) newPlayer.scores = new List<int>();
newPlayer.scores.Add(int.Parse(splitLine[1]));
}
}
}
}
string[] playersNames = Player.players.Select(x => x.name).ToArray();
Console.WriteLine(string.Join(" , ", playersNames));
Console.ReadLine();
}
}
public class Player
{
public static List<Player> players = new List<Player>();
public string name { get; set; }
public List<int> scores { get; set; }
}
}