How to convert a String[] to int[] in C# and .NET 2.0?

前端 未结 6 2013
名媛妹妹
名媛妹妹 2020-12-15 04:37

I\'ve got :

string[] strArray = new string[3] { \"1\", \"2\", \"12\" };

And I want something like this

int[] intArray = Con         


        
6条回答
  •  余生分开走
    2020-12-15 05:29

    Something like this, but you want to keep a bit of error checking ( my if is really lame, just as example ):

    static private int[] toIntArray(string[] strArray)
    {
        int[] intArray = new int[strArray.Length];
        for (int index = 0; index < strArray.Length; index++)
        {
                intArray[index] = Int32.Parse(strArray[index]);
        }
        return intArray;
    }
    

    And use it like this:

    string[] strArray = new string[3] { "1", "2", "12" };
    int[] interi = toIntArray(strArray);
    

提交回复
热议问题