索引超出了数组的范围 [英] Index was outside the bound of array

查看:119
本文介绍了索引超出了数组的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string str = textBox1.Text;
textBox1.Text = "";
int i = str.Length;
for (int j = 0; j < i - 1; j++)
{
    textBox1.Text += Convert.ToString(str[i]);
}



以上是sapce编码....当我要运行而不是错误索引超出数组范围即将来临


Above is back sapce coding....when i am going to run than error "Index was outside the bound of array" is coming

推荐答案

你必须将字符串转换为char数组,然后进行foreach循环。



示例:



You have to convert the string into char array and then do foreach loop.

Example:

if (TextBox1.Text != string.Empty)
            {
                string str = TextBox1.Text;
                char[] array = str.ToCharArray();
                TextBox1.Text = "";

                foreach (char c in array)
                {
                    TextBox1.Text += Convert.ToString(c);
                }
            }





你不觉得代码会be:



Hi
Don''t you think the code will be :

textBox1.Text += Convert.ToString(str[j]); 



而不是


instead of

textBox1.Text += Convert.ToString(str[i]);


这篇关于索引超出了数组的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆