如何使用文本框中的每个数字 [英] how to use each digit in textbox

查看:86
本文介绍了如何使用文本框中的每个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想使用文本框中的每个数字.例如,如果文本框中的输入为123456
我想取第一位数字(这里是1),然后用它做点什么.
我将输入转换为charArray并使用了每个输入,但显示的是48个而不是第一个元素(这里是1),而第二个则显示49个.
我该怎么办?谢谢


i want to use each digit in textbox.for example if input in textbox is 123456
i want to take firstdigit (here is 1) and do something wih it.
i converted the input to charArray and used each one,but it shows 48 instead of first element(here is 1) and 49 for second element.
what should i do?thanks

推荐答案

您可以尝试
You could try
foreach (char a in TextBox1.Text)
    {
       int i = System.Convert.ToInt32(System.Char.ConvertFromUtf32(a));
    }


不要担心.这只是显示字符值的另一种方式:"1"等于49,即十六进制为31.

[edit]
考虑一下,您可能想要使用转换为int或类似字符的字符:
Don''t worry about it. It is just a different way of showing the character value: ''1'' is equal to 49, or 31 in Hex.

[edit]
Thinking about it, you probably want to use the character converted to an int or similar:
string s = "12345";
char[] ac = s.ToCharArray();
foreach (char c in ac)
    {
    int i = (int) char.GetNumericValue(c);
    Console.WriteLine(i);
    }

您可以使用

int i = int.Parse(s);


转换整个原始字符串 [/edit]



"arashmobileboy-18分钟前
抱歉,现在如何将它们放入数组?
回复
OriginalGriff-10分钟前
您要使用哪种数组?您实际上想达到什么目的?因为我忍不住想你正在朝一个奇怪的方向前进...
回复
arashmobileboy-6分钟前
我想在文本框中输入数字数组,例如i [0] +15"


好的-我不知道为什么要这么做,但是...


[/edit]



"arashmobileboy - 18 mins ago
sorry,now how can i put them into an array?
Reply
OriginalGriff - 10 mins ago
What kind of array do you want to use? What are you actually trying to achieve? Because I can''t help but think you are going in a odd direction...
Reply
arashmobileboy - 6 mins ago
i want to make an array of input numbers in textbox,and for example do i[0]+15"


OK - I don''t know why you want to do that, but...

string s = "12345";
char[] ac = s.ToCharArray();
int[] digits = new int[ac.Length];
for (int i = 0; i < ac.Length; i++)
    {
    digits[i] = (int) char.GetNumericValue(ac[i]);
    }
Console.WriteLine(digits[0] + 15);


这篇关于如何使用文本框中的每个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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