C# 如何获取字符串列表c中的字段#
目前在c#中,我有一个文本文件中每行的字符串列表。 在此文件中,有一些字段用逗号分隔C# 如何获取字符串列表c中的字段#,c#,list,C#,List,目前在c#中,我有一个文本文件中每行的字符串列表。 在此文件中,有一些字段用逗号分隔 string[] logFile = File.ReadAllLines(path); List<string> logList = new List<string>(logFile); 所以我想要的是获得每个字段,并将其保存到其适当的变量中 感谢您的帮助。您可以在各行之间循环,并使用在单独的字段中拆分各行 foreach(var field in logList) { str
string[] logFile = File.ReadAllLines(path);
List<string> logList = new List<string>(logFile);
所以我想要的是获得每个字段,并将其保存到其适当的变量中
感谢您的帮助。您可以在各行之间循环,并使用在单独的字段中拆分各行
foreach(var field in logList)
{
string [] allFieldsInLine = field.Split(',');
}
allFieldsInLine
中的顺序将与您所在行中的顺序相同
在这方面:
1,法律,A,A,0.972177,0,0,-3276732767600.0,1,p
IAW
将位于allFieldsInLine[1]
在allFieldsInLine[3]
将是”
或字符串。空的而不是列表(字符串),我将创建一个对象类,它接受12个不同的参数(字符串类型),然后将它们添加到列表(输入)输入列表中。下面是一些仅包含2个参数的示例代码。尝试在您自己的程序中实现这一点
创建对象类:
class Input
{
private string thing1;
private string thing2;
public Input(string thing1, string thing2)
{
this.thing1 = thing1;
this.thing2 = thing2;
}
public string Thing1 { get => thing1; set => thing1 = value; }
public string Thing2 { get => thing2; set => thing2 = value; }
}
因此,现在您已经创建了对象,您可以创建此对象的列表
List<Input> inputList = new List<Input>();
现在,每当您想要引用这些值时,您可以使用foreach循环遍历列表,或者使用
inputList.ElementAt[#].thing2
希望这有帮助 它是csv文件吗?在这种情况下,会有专门的读卡器。COMTRADE Standar(.CFG)的配置文件格式是否总是统一的?一些示例输入将有助于确定问题的答案为何为-1,人们需要给新来者一个机会,然后在否定点上记下他们的问题。之后可以在每个字段和.Trim()上迭代表的空白string@Lukas这可以在分割中删除,但我不确定第4个值,可能是故意空的是故意空的,我要试试这个。谢谢
StreamReader reader = new StreamReader(text file);
string line;
while ((line = reader.ReadLine()) != null)
{
string[] useThis = line.Split(',');
inputList.Add(new Input(useThis[0], useThis[1]);
}
inputList.ElementAt[#].thing2