C# string[]转int[]

法一:逐项复制

        public static int[] ToIntArray(this string[] strArray)
        {
            if (strArray == null || strArray.Length == 0)
            {
                Debug.LogError("error, input stringArray is null or length=0");
            }
            int[] result = new int[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                try
                {
                    result[i] = int.Parse(strArray[i]);
                }
                catch (Exception ex)
                {

                    Debug.ThrowException("StringArrayToIntArray exception:" + ex.Message);
                }

            }
            return result;
        }

法二:.NET3.0提供有方法Array.ConvertAll

            string a = "0|0|0";
            string[] strs = a.Split('|');
            int[] ints = Array.ConvertAll(strs, int.Parse);

你可能感兴趣的:(C#)