Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从.txt文件导入x和y坐标_C#_Coordinates - Fatal编程技术网

C# 从.txt文件导入x和y坐标

C# 从.txt文件导入x和y坐标,c#,coordinates,C#,Coordinates,我需要从我的项目的文本文件中读取坐标,但是文本文件是这样的 1 37.4393516691 541.2090699418 2 612.1759508571 494.3166877396 3 38.1312338227 353.1484581781 前面有更多的空间。我有我尝试过的代码,但我无法让分隔符工作 1 1150.0 1760.0 2 630.0 1660.0 3 40.0 2090.0 守则: string[

我需要从我的项目的文本文件中读取坐标,但是文本文件是这样的

1 37.4393516691 541.2090699418

2 612.1759508571 494.3166877396

3 38.1312338227 353.1484581781
前面有更多的空间。我有我尝试过的代码,但我无法让分隔符工作

      1    1150.0  1760.0

      2     630.0  1660.0

      3      40.0  2090.0
守则:

 string[] cityPositions = File.ReadAllLines(ofd.FileName);
                foreach (string cityP in cityPositions)
                {
                    int startIndexX = cityP.IndexOf("  ", StringComparison.CurrentCultureIgnoreCase) + 3;
                    int endIndexX = cityP.IndexOf(" ", StringComparison.CurrentCultureIgnoreCase);
                    int X = int.Parse(cityP.Substring(startIndexX, endIndexX - startIndexX));

                    int startIndexY = cityP.IndexOf(" ", StringComparison.CurrentCultureIgnoreCase) + 3;
                    int endIndexY = cityP.IndexOf("", StringComparison.CurrentCultureIgnoreCase);
                    int Y = int.Parse(cityP.Substring(startIndexY, endIndexY - startIndexY));
                    create_City(new Point(X, Y));
                }

首先,您的数据类型不匹配。您应该使用double作为坐标,并使用Split方法,例如:

double X = double.Parse(cityP.Split()[1], CultureInfo.InvariantCulture);
double Y = double.Parse(cityP.Split()[2], CultureInfo.InvariantCulture);
没有给定参数的Split函数将被空格分割

编辑

问题中不清楚,但是,如果您的线条没有相同的图案,并且有不同的空间,请使用如下拆分方法:

double X = double.Parse(cityP.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1], CultureInfo.InvariantCulture);

如果要使用正则表达式,请尝试以下操作:

        String expression = @"(\d)([\d\.]*)([\d\.]*)";

        Regex r = new Regex(expression);

        foreach (String cityPosition in cityPositions)
        {
            MatchCollection mc = r.Matches(cityPosition);
            int index = Convert.ToInt32(mc[0].Value);
            decimal coOrdX = Convert.ToDecimal(mc[1].Value);
            decimal coOrdY = Convert.ToDecimal(mc[2].Value);

            Console.WriteLine(String.Format("{0} {1} {2}", index, coOrdX, coOrdY));
        }
此代码将生成以下输出:

1 1150.0 1760.0
2 630.0 1660.0
3 40.0 2090.0

它应该忽略值之间不同的空间量,使解析文件中的三个不同值更容易。

您可以用一个空格替换多个空格,而不是使用索引,如

    RegexOptions options = RegexOptions.None;
    Regex regex = new Regex("[ ]{2,}", options);     
    cityP = regex.Replace(cityP, " ");
然后使用split获取坐标

    var x = double.Parse(cityP.Split()[1]);
    var y = double.Parse(cityP.Split()[2]);

您可以逐个字符循环遍历每个字符串,每次都忽略空格,第一个数字是索引,第二个和第三个数字是您感兴趣的坐标值。或者,您可以使用正则表达式仅提取数字如果使用正则表达式更安全,请尝试
var cords=Regex.Split(cityP,@“\s”)
\s
匹配所有空格。