代码之家  ›  专栏  ›  技术社区  ›  Kenta

使用键-值对解析字符串,其中键和值c之间没有字符#

  •  -1
  • Kenta  · 技术社区  · 6 年前

    我试图逐行解析文件,每次都将变量设置为行中的值。

    示例行为:

    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;
    

    第三条线只是可能出现的另一条线,但它们都非常相似。如果该键未出现在字符串中,则将值设置为100000。

    我尝试使用如下代码:

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

    但第一行的结果是:

    z = 0.500 F7800.000
    

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

    2 回复  |  直到 6 年前
        1
  •  2
  •   Jeff Anderson    6 年前

    这应该让你开始。。。

            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;
                            }
                        }
                    }
                }
            }
    
        2
  •  0
  •   Rufus L    6 年前

    您可以做的一件事是创建一个简单的类来表示您关心的数据 X ,则, Y Z 。然后您可以创建 static 方法,该方法知道如何从字符串创建类的实例。

    例如:

    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));
    }