解析字符串int数组 [英] Parse string to int array

查看:93
本文介绍了解析字符串int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的工作增加我的C#的知识和我想做一个简单的任务,但我发现很难。

Hi there I am working on increasing my knowledge of C# and I want to do a simple task but am finding it difficult.

我要一个字符串的每个字符解析到一个int数组。

I want to parse each character of a string to an int array.

    int[] d = new int[10];
     private void button1_Click(object sender, EventArgs e)
    {
        s = textBox1.Text;

        d[2] = Convert.ToInt32(Convert.ToString(s[1]));
        d[3] = Convert.ToInt32(Convert.ToString(s[2]));
        d[4] = Convert.ToInt32(Convert.ToString(s[3]));
        d[5] = Convert.ToInt32(Convert.ToString(s[4]));
        .....

我知道code是错误的,但我想表现的例子。已经看过谷歌在过去一小时,但发现人想以显示新的行字符,并且使用if语句进行解析。我想类似上面的东西。可以这样做?感谢您的时间。

I know the code is wrong but I wanted to show an example. Have looked at Google for the past hour but have found people wanting to display characters in new lines and using if statements to parse. I want something similar to above. Can this be done? Thank you for your time.

我发现这行code的在Java中,这正是我想要做的但有一个equivilant?

I found this line of code in Java which is exactly what I want to do but is there an equivilant?

    d1 = Integer.parseInt(String.valueOf(s.charAt(0)));

我现在有一个新的问题,当我提交了一些不管有多少个数字,我把它会回来与索引数组的范围之外。

I now have a new problem that when i submit a number no matter how many numbers i put in it will come back with "Index was outside the bounds of the array."

我还需要将其限制在10整数数组ð。

I also need to limit it to 10 integers in the array d.

推荐答案

错误括号,字符串中访问的字符。

Wrong brackets, to access the character within string.

d[2] = Convert.ToInt32(Convert.ToString(s(1)));
                                         ^

使用,

d[2] = Convert.ToInt32(Convert.ToString(s[1]));

这篇关于解析字符串int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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