c#将字符串转换为2个双精度数

时间:2012-01-10 23:09:21

标签: c# string class split double

我仍然是c#编程的新手,所以我试图创建一个类方法。 但它不起作用。

那么我想做什么: 我得到一些名称为var的字符串。 x和y数。 例如,字符串将是这样的:“x02y42”

所以我想在我的应用程序(wpf窗口)中有一个函数,我可以在那里发回该字符串,并获得2个双精度返回,如:“double = 02”和“double = 42”(因此不是字符串)。

所以我上了一堂课, 在这堂课中,我制作了这段代码:

public double x,y(string parm)
{
    //input shall be: string s = x12y04;
    //inp s looks like x12y04

    //splut the string on the Y (so this tech should also work for x89232y329)
    //so this will create res[0] that is x89232 and an res[1] that is 329
    string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries);
    //now for the x in res[0], we replace it for a 0 so it are all numbers
    string resx = res[0].Replace("x", "0");

    //now we will get the strings to doubles so we can really start using them.
    double x = double.Parse(resx);
    double y = double.Parse(res[1]);

    //get the values back
    return x, y;

}

现在(ofc)它没有用。 但如上所述,我仍然是c#的新手。 那么我在这里做错了什么? 我可以把它放在1个功能中吗? 那么在我的应用程序中我可以用某种方式调用它?

10 个答案:

答案 0 :(得分:3)

你必须有Python背景,我可以从你的代码中推断出来。

在4.0之前的C#中,无法以 pythonic 方式返回多个值(元组)。您需要返回一个数组或通过out参数返回值。 (除非你定义代表元组的类)。

在C#4.0中,您可以按照评论中的说明返回Tuple<double, double>

答案 1 :(得分:3)

如果你真的想使用元组,请使用F#而不是C#。在C#中,你必须使用一些代表元组的类型;这可以是Point类型或Tuple<,>类型。

更改

public double x,y(string parm) 
...
return x, y;

public Point GetXY(string parm) 
...
return new Point(x, y);

public Tuple<double, double> GetXY(string parm) 
...
return Tuple.Create(x, y);

答案 2 :(得分:1)

您必须返回包含这两个值的数组。 E.g:

public double[] Convert(string parm)
{
    //input shall be: string s = x12y04;
    //inp s looks like x12y04

    //splut the string on the Y (so this tech should also work for x89232y329)
    //so this will create res[0] that is x89232 and an res[1] that is 329
    string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries);
    //now for the x in res[0], we replace it for a 0 so it are all numbers
    string resx = res[0].Replace("x", "0");

    //now we will get the strings to doubles so we can really start using them.
    double x = double.Parse(resx);
    double y = double.Parse(res[1]);

    //get the values back
    return new double[] { x, y };

}

答案 3 :(得分:0)

你不能从方法中返回2个对象

你应该返回一个double或一个元组或一个点的数组(我认为你正在使用坐标)

无论如何

而不是string.split最好使用indexof(“x”)和“y”然后使用substring,这样你就可以检查传递的字符串并避免indexoutofrange异常

    public double[] GetCoordinates(string inputValue)
    {
        string normalized = inputValue.ToLower();
        int indexX = normalized.IndexOf("x")+1;
        int indexY = normalized.IndexOf("y")+1;

        if (indexX == 0 || indexY == 0) 
        {
            throw new ArgumentException();
        }

        //if string is y45x22 i reverse them
        if (indexX > indexY) 
        {
            indexX ^= indexY;
            indexY ^= indexX;
            indexX ^= indexY;
        }
        string xstring = normalized.Substring(indexX, normalized.Length - indexY);
        string ystring = normalized.Substring(indexY, normalized.Length - indexY);

        double[] rtn = new double[2];

        if (!double.TryParse(xstring, out rtn[0]) || !double.TryParse(ystring, out rtn[1])) 
        {
            throw new ArgumentException();
        }
        return rtn;
    }

答案 4 :(得分:0)

    class Point
    {
        public Point(int x, int y) { this.x = x; this.y = y; }
        public double x { get; set; }
        public double y { get; set; }
    }

public Point ParseString(string parm)
{
    //input shall be: string s = x12y04;
    //inp s looks like x12y04

    //splut the string on the Y (so this tech should also work for x89232y329)
    //so this will create res[0] that is x89232 and an res[1] that is 329
    string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries);
    //now for the x in res[0], we replace it for a 0 so it are all numbers
    string resx = res[0].Replace("x", "0");

    //now we will get the strings to doubles so we can really start using them.
    double x = double.Parse(resx);
    double y = double.Parse(res[1]);

    //get the values back
    return new Point(x,y);

}

答案 5 :(得分:0)

我稍微简化了代码:

// C# method names cannot contain character ,
public double[] Parse(string param)
{
    string[] res = param.Split(new string[] { "x",  "y" }, StringSplitOptions.RemoveEmptyEntries);

    double x = double.Parse(res[0]);
    double y = double.Parse(res[1]);

    //get the values back as array
    return new double[] { x, y };
}

答案 6 :(得分:0)

这就是我要做的......

    static Tuple<Double, Double> ParseCoordinates(string parm)
    {
        string[] res = parm.Replace("x","")
                           .Replace("y",",")
                           .Split(",".ToCharArray());
        double x, y;
        double.TryParse(res[0], out x);
        double.TryParse(res[1], out y);

        return new Tuple<double, double>(x, y);

    }

如果您无法访问c#4 ...

    static bool ParseCoordinates(string parm, out double x, out double y)
    {
        string[] res = parm.Replace("x","")
                           .Replace("y",",")
                           .Split(",".ToCharArray());

        bool parseX = double.TryParse(res[0], out x);
        bool parseY = double.TryParse(res[1], out y);

        return parseX && parseY;

    }

答案 7 :(得分:0)

您可以创建如下所示的对类 -

    public class Pair<T, U> {
      public Pair() {
      }

      public Pair(T first, U second) {
        this.First = first;
        this.Second = second;
      }

     public T First { get; set; }
     public U Second { get; set; }
    };

并将返回类型设置为方法中的pair对象并返回         新对(x,y);

答案 8 :(得分:0)

首先,“x,y”不是有效的方法名称。其次,一种方法只能返回一件事。关键是返回一个代表两个值的对象。类似于System.Drawing.Point()或Tuple()。

这是适合您的事情:

    public Tuple<double, double> GetXY(string parm)
    {
        Regex regex = new Regex(@"x(?<x>\d+)y(?<y>\d+)");

        Match match = regex.Match(parm);
        if (!match.Success)
            return new Tuple<double, double>(0, 0);

        double x, y;
        double.TryParse(match.Groups["x"].Value, out x);
        double.TryParse(match.Groups["y"].Value, out y);

        return new Tuple<double, double>(x, y);
    }

您可以通过执行以下操作来测试它:

        Console.WriteLine(GetXY("x53y2384"));

答案 9 :(得分:0)

如果您真的想要这种类型的风格,可以使用输出参数。但是,我同意大多数其他海报,因为你的x / y数据应该作为一个包含x和y数据的单独类返回。

    static void Main(string[] args)
    {
        double x, y;

        parse("x12y04", out x, out y);

        //x and y now contain 12 and 4
    }

    static void parse(String parm, out double x, out double y)
    {
        string[] res = parm.Split(new string[] { "y" }, StringSplitOptions.RemoveEmptyEntries);
        string resx = res[0].Replace("x", "0");

        x = double.Parse(resx);
        y = double.Parse(res[1]);
    }