Index was outside the bounds of the array.

来源:互联网 发布:供应链 saas软件 编辑:程序博客网 时间:2024/05/25 08:14
背景今天写了这么一个函数:
private string DateFormatConversion(string str)        {                string year = "";                char[] pstr = str.ToCharArray();                for (int k = 0; k < 4; k++)                {                    year += pstr[k];                }                return year;        }
当str传入值如空的时候,就会提示“Index was outside the bounds of the array.”错误。
解决方案:
在处理str之前,先判断str.Length是否大于零
0 0