整数字符串到 int [] 数组 C#

这个问题是不同的,因为它解决了在添加到整数数组时将 char 转换为 int 的问题。


下面的一段代码,我试图在 C# 中将一个整数字符串实现为一个 int[] 数组。


我想要的输出是一个数组:


12345678910

这是我的代码,但是,它的输出不是我想要的:


string numbers = "12345678910";


int[] array = new int[numbers.Length];


for (int i = 1; i <= numbers.Length; i++)

{

    array[i - 1] = numbers[i-1];

}


foreach(var y in array)

{

    Console.Write(y);

}

给定代码的输出:


4950515253545556574948

有人能告诉我为什么我得到这个输出,我能做些什么来解决这个问题?谢谢!


编辑:更改了我的代码,它可以使用:


for (int i = 0; i < numbers.Length; i++)

{

     array[i] = int.Parse(numbers[i].ToString());

}


HUX布斯
浏览 128回答 1
1回答

尚方宝剑之说

首先,我认为您无法使用这种方法区分两位数。我指的是您的代码的这一部分: 遍历您的字符串字符并解析为 Int (如果需要的话)(credit)(credit)string numbers = "12345678910";&nbsp;foreach (char character in yourString)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x = (int)Char.GetNumericValue(character);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //code to add to your array of ints&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP