法一:逐项复制
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);
本文介绍了两种将字符串数组转换为整数数组的方法。第一种是通过逐项复制并使用int.Parse进行类型转换,第二种是利用.NET3.0中Array.ConvertAll方法结合int.Parse实现快速转换。这些方法在处理大量数据时尤为实用。
928

被折叠的 条评论
为什么被折叠?



