C#解析文本文件中的元素

C#解析文本文件中的元素,c#,file,parsing,text,C#,File,Parsing,Text,我有一个包含以下内容的文本文件: 139862,2 - 455452,6 - 13:24:53 139860,8 - 455452,2 - 13:25:13 139859,3 - 455452,2 - 13:25:33 如您所见,文本文件的每行有3个元素。我希望能够将这些保存为变量 我可以为我希望能够保存的每个变量添加前缀,这样文本将如下所示: X:139862,2 - Y:455452,6 - D:13:24:53 X:139860,8 - Y:455452,2 - D:13:2

我有一个包含以下内容的文本文件:

139862,2 - 455452,6 - 13:24:53 

139860,8 - 455452,2 - 13:25:13 

139859,3 - 455452,2 - 13:25:33
如您所见,文本文件的每行有3个元素。我希望能够将这些保存为变量

我可以为我希望能够保存的每个变量添加前缀,这样文本将如下所示:

X:139862,2 - Y:455452,6 - D:13:24:53 

X:139860,8 - Y:455452,2 - D:13:25:13 

X:139859,3 - Y:455452,2 - D:13:25:33
我已经得到的是逐行读取文本文件的编码,它看起来像:

    string line;
    System.IO.StreamReader file = new System.IO.StreamReader(filePath);
    while ((line = file.ReadLine()) != null)
    {
        // Here I'd like to parse the text file and add them to a list for example
    }
提前感谢

试试这个:

string[] stringParsed = line.split(" - ");

您可以使用
string.Split
float.Parse
DateTime.Parse

string line;
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
    string[] parts = line.Split('-');
    float x = float.Parse(parts[0].Trim(), NumberFormatInfo.InvariantInfo);
    float y = float.Parse(parts[1].Trim(), NumberFormatInfo.InvariantInfo);
    DateTime d = DateTime.Parse(parts[2].Trim(), DateTimeFormatInfo.InvariantInfo);

    Console.WriteLine("X: {0}, Y: {1}, D: {2}", x, y, d);
}
请注意,如果字符串无效,
*.Parse
将引发异常。因此,您可能需要使用
TryParse
来代替,或者将其包装在
try/catch
块中

我使用
*FormatInfo.InvariantInfo
确保正确解析

如果你有这样一个数据类

public class Record
{
    public float X { get; set; }
    public float Y { get; set; }
    public DateTime D { get; set; }
}
您可以通过循环生成这些记录的列表:

List<Record> records = new List<Record>();
string line;
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
    string[] parts = line.Split('-');
    records.Add(new Record {
        X = float.Parse(parts[0].Trim(), NumberFormatInfo.InvariantInfo),
        Y = float.Parse(parts[1].Trim(), NumberFormatInfo.InvariantInfo);
        D = DateTime.Parse(parts[2].Trim(), DateTimeFormatInfo.InvariantInfo)});
}
List记录=新列表();
弦线;
System.IO.StreamReader file=新的System.IO.StreamReader(文件路径);
而((line=file.ReadLine())!=null)
{
string[]parts=line.Split('-');
记录。添加(新记录){
X=float.Parse(部分[0].Trim(),NumberFormatInfo.InvariantInfo),
Y=float.Parse(parts[1].Trim(),NumberFormatInfo.InvariantInfo);
D=DateTime.Parse(部分[2].Trim(),DateTimeFormatInfo.InvariantInfo)});
}

这个问题的解决方案可以非常直接地完成:

string line;
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
List<string> newlines = new List<string>(); //here to store your List of string
string[] delimiters = new string[] { " - " }; //declared only once outside of while loop
while ((line = file.ReadLine()) != null)
{
    string[] words = line.Split(delimiters);
    newlines.Add("X:" + words[0] + " - Y:" + words[1] + " - Z:" + words[2]);
}
字符串行;
System.IO.StreamReader file=新的System.IO.StreamReader(文件路径);
列表换行符=新列表()//这里存储您的字符串列表
字符串[]分隔符=新字符串[]{“-”}//仅在while循环外声明一次
而((line=file.ReadLine())!=null)
{
string[]words=line.Split(分隔符);
换行。添加(“X:+words[0]+”-Y:+words[1]+”-Z:+words[2]);
}

您的最终结果将出现在
换行符中

我使用了StringReader,但您可以用StgreamReader替换它来读取文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "139862,2 - 455452,6 - 13:24:53\n" +
            "139860,8 - 455452,2 - 13:25:13\n" +
            "139859,3 - 455452,2 - 13:25:33\n";

            string inputLine = "";
            StringReader reader = new StringReader(input);
            while((inputLine = reader.ReadLine()) != null)
            {
                string[] inputArray = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Console.WriteLine("X:{0} {1} Y:{2} {3} D:{4}", inputArray[0], inputArray[1], inputArray[2], inputArray[3], inputArray[4]);
            }
            Console.ReadLine();
        }
    }
}

您在实际处理数据方面做了哪些努力?请看