如何从int类型的数值上取出各个位上的数?
   要是string类型呢?从string上去出数再转换成int类型这样可以吗?要是可以又该从string上如何取出各个位上的数?
   注意数的位数不固定!数是随机的!

解决方案 »

  1.   

    int i = 123456;
    Console.WriteLine(i%10);//个位
    Console.WriteLine(i/10%10);//百位
      

  2.   

    int i = 123456;
    Console.WriteLine(i%10);//个位
    Console.WriteLine(i/10%10);//百位
    ToCharArray
      

  3.   

    直接ToCharArray最方便了
    ToCharArray 主要将字符串拆分为单个字符输出
    字符串类 System.String 提供了一个 void ToCharArray() 方法,该方法可以实现字符串到字符数组的转换。如下例:
    private void TestStringChars() {string str = "mytest";char[] chars = str.ToCharArray();this.textBox1.Text = "";this.textBox1.AppendText("Length of "mytest" is " + str.Length + " ");this.textBox1.AppendText("Length of char array is " + chars.Length + " ");this.textBox1.AppendText("char[2] = " + chars[2] + " ");}
    例中以对转换转换到的字符数组长度和它的一个元素进行了测试,结果如下:
    Length of "mytest" is 6Length of char array is 6char[2] = t