解析具有键值对的字符串,其中键和值 c# 之间没有字符

Parse a string with key-value pairs where there is no char between key and value c#

我正在尝试逐行解析文件,每次都将变量设置为等于行中的值。

示例行是:

G1 Z0.500 F7800.000
G1 X-0.336 Y13.564 F7800.000
G1 X3.205 Y13.493 E3.63071 F1800.000

我会有这样的变量:

double x;
double y;
double z;

假设我解析了第一行,我的输出应该是:

x = 100000;
y = 100000;
z = 0.5;

如果我解析第二行,我的输出应该是:

x = -0.336;
y = 13.564;
z = 100000;

第三行只是可能出现的另一行,但都非常相似。如果密钥未出现在字符串中,则值设置为 100,000。

我尝试使用这样的代码:

char[] delimiters = { 'X', 'Y', 'Z'};
string[] words = line.Split(delimiters);

但第一行返回:

z = 0.500 F7800.000

为这些键值对解析每一行的最佳方法是什么?

这应该让你开始......

        using (StreamReader sr = new StreamReader("data.txt"))
        {
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                if (line != null)
                {
                    string[] items = line.Split(' ');
                    decimal? x, y, z = null;
                    for (int i = 1; i < items.Length; i++)
                    {
                        if (items[i].ToLower().StartsWith("x"))
                        {
                            x = decimal.Parse(items[i].Substring(1));
                            Console.WriteLine($"x = {x}");
                        }
                        else if (items[i].ToLower().StartsWith("y"))
                        {
                            y = decimal.Parse(items[i].Substring(1));
                            Console.WriteLine($"y = {y}");
                        }
                        else if (items[i].ToLower().StartsWith("z"))
                        {
                            z = decimal.Parse(str);
                            Console.WriteLine($"y = {z}");
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
        }

您可以做的一件事是创建一个简单的 class 来表示您关心的数据,这些数据显示为 XYZ。然后你可以创建一个 static 方法,该方法知道如何从字符串创建 class 的实例。

例如:

public class XYZData
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }

    public static XYZData Parse(string input)
    {
        var xyzData = new XYZData { X = 100000, Y = 100000, Z = 100000 };

        if (string.IsNullOrWhiteSpace(input)) return xyzData;

        var parts = input.Split();

        foreach (var part in parts)
        {
            double result;

            if (part.Length < 2 || 
                !double.TryParse(part.Substring(1), out result)) 
            {
                continue;
            }

            if (part.StartsWith("X", StringComparison.OrdinalIgnoreCase))
            {
                xyzData.X = result;
                continue;
            }
            if (part.StartsWith("Y", StringComparison.OrdinalIgnoreCase))
            {
                xyzData.Y = result;
                continue;
            }
            if (part.StartsWith("Z", StringComparison.OrdinalIgnoreCase))
            {
                xyzData.Z = result;
                continue;
            }
        }

        return xyzData;
    }
}

然后您可以在读取文件时填充这些内容的列表:

var filePath = @"f:\public\temp\temp.txt";

var data = new List<XYZData>();

foreach (var fileLine in File.ReadLines(filePath))
{
    data.Add(XYZData.Parse(fileLine));
}