从字符串中获取字符会返回意外数字吗? [英] Getting a character from a string is returning an unexpected number?

查看:59
本文介绍了从字符串中获取字符会返回意外数字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,其中包含从访问表中获取的汽车的ID号和型号.当从列表框中选择汽车时,我有一些文本框将填充该表中的数据.要获取所选汽车的ID号,我使用以下代码:

I have a list box that contains the ID number and model of a number of cars, taken from an access table. I have some text boxes that will be filled with the data from that table when a car is selected from the list box. To get the id number of the selected car, I'm using this code:

int idNum = listBox1.SelectedItem.ToString()[0];

应该获得项目字符串中的第一个字符(它的ID号).当我选择列表中的第一项时,我得到的是49.我首先想到的是我没有将其强制转换为int,因此49可能代表该数字的某些字符代码,但是

Which should get the first character in the item string (it's ID number). When I select the first item in the list, what I am instead getting is 49. My first thought was that I wasn't casting it to an int, so maybe the 49 represents some character code for the number, however

Convert.ToInt16(listBox1.SelectedItem.ToString()[0])

返回完全相同的数字.第二项是50,依此类推.

Returns the exact same number. The second item is 50, and so on and so forth.

无论如何,我想改变从此char转换中获取ID号的方式,因为在我碰巧碰到10辆车就不起作用了,但是为什么我会得到这么大的,始终如一的偏移量,和(也许是最令人困惑的)来自ac#的java的charAt[0]等效数字?

I want to be changing how I get the ID number from this char conversion anyway, as it's just occurred to me that once I hit 10 cars it won't work, but why am I getting such a large, consistently offset, and (perhaps most bafflingly of all) multi-digit number from a c# equivalent of java's charAt[0]?

我一直遇到的特定错误是there is no row at position 49,当我尝试获取mydataset.Tables["mytable"].Rows[idNum]["CarID"].ToString();的值时会弹出该错误.现在,我看到了可能导致此问题的一些原因,我完全知道Rows[idNum]["CarID"]是否是正确的语法,并且很惊讶地发现我的猜测有效,但这仍然是一个非常奇怪的问题.

The specific error I've been getting is that there is no row at position 49, which pops up when I try to get the value of mydataset.Tables["mytable"].Rows[idNum]["CarID"].ToString();. Now, I can see a few things that could be causing this issue, I have absolutely know idea if Rows[idNum]["CarID"] is the correct syntax, and was very surprised to see that my guess worked, but it's still a very weird problem.

推荐答案

您需要获取Char的数值:

int idNum = (int)Char.GetNumericValue(listBox1.SelectedItem.ToString()[0]);

int idNum = listBox1.SelectedItem.ToString()[0]);返回字符'1'ASCII(示例)为49.

While int idNum = listBox1.SelectedItem.ToString()[0]); return the ASCII of Character '1' (example) which is 49.

这是来自 Microsoft 中用于Convert.ToInt16的实际代码:

This is the actual code from Microsoft for Convert.ToInt16:

public static short ToInt16(char value) 
{
    // Some validations
    return (short)value;
}

Convert.ToInt16进行Explicit转换,这将获得该Character值的ASCII.

Convert.ToInt16 for a char does an Explicit conversion, which gets the ASCII for that Character Value.

用于处理多个数字:

string str = "37abcdef";
string myStrNumber  = Regex.Match(str, @"\d+").Value;

int idNum2;

if (myStrNumber.Length > 0)
    idNum2 = Convert.ToInt32(myStrNumber);
else
{
    // Handle Error
}

或者不使用正则表达式:

string str = "37abcdef";
string myStrNumber = "";

for(int i = 0; i < str.Length; i++)
{
    if (Char.IsNumber(str[i]))
        myStrNumber += str[i];
}

int idNum2;

if (myStrNumber.Length > 0)
    idNum2 = Convert.ToInt32(myStrNumber);
else
{
    // Handle Error
}

这篇关于从字符串中获取字符会返回意外数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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