C# 将字符串转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/887586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to integer
提问by tintincutes
I need help with my code. I would like to write only numbers/integers in my textbox and would like to display that in my listbox.
我的代码需要帮助。我想在我的文本框中只写数字/整数,并想在我的列表框中显示它。
Is my code below in order? This seems to give an error.
我的代码在下面吗?这似乎给出了错误。
int yourInteger;
string newItem;
newItem = textBox1.Text.Trim();
if (newItem == Convert.ToInt32(textBox1.Text))
{
listBox1.Items.Add(newItem);
}
==== Update:
==== 更新:
This is how my code looks like now. My question is, can listBox handle the data type "long"? Because when I entered the number 20,000,000 I just got an hour glass for 20 minutes. But when I tried this one with the console, I got the answer. So I'm not sure what kind of element can handle data type "long".
这就是我的代码现在的样子。我的问题是,listBox 可以处理数据类型“long”吗?因为当我输入数字 20,000,000 时,我只得到了一个 20 分钟的沙漏。但是当我在控制台上尝试这个时,我得到了答案。所以我不确定什么样的元素可以处理“long”数据类型。
string newItem;
newItem = textBox1.Text.Trim();
Int64 num = 0;
if(Int64.TryParse(textBox1.Text, out num))
{
for (long i = 2; i <= num; i++)
{
//Controls if i is prime or not
if ((i % 2 != 0) || (i == 2))
{
listBox1.Items.Add(i.ToString());
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
采纳答案by cjk
Use this:
用这个:
int yourInteger;
string newItem;
newItem = textBox1.Text.Trim();
Int32 num = 0;
if ( Int32.TryParse(textBox1.Text, out num))
{
listBox1.Items.Add(newItem);
}
else
{
customValidator.IsValid = false;
customValidator.Text = "You have not specified a correct number";
}
This assumes you have a customValidator.
这假设您有一个 customValidator。
回答by Mehrdad Afshari
int result = int.Parse(textBox1.Text.Trim());
If you want to check for validity:
如果要检查有效性:
int result;
if (int.TryParse(textBox1.Text.Trim(), out result)) // it's valid integer...
// int is stored in `result` variable.
else
// not a valid integer
回答by smok1
Use int.TryParse() to check if string contains integer value.
使用 int.TryParse() 检查字符串是否包含整数值。
回答by Pat
Are you checking for an empty string?
您是否正在检查空字符串?
int yourInteger;
string newItem;
newItem = textBox1.Text.Trim();
if(newItem != string.Empty)
{
if ( newItem == Convert.ToInt32(textBox1.Text))
{
listBox1.Items.Add(newItem);
}
}
回答by Anton Gogolev
textBox1.Text
may not contain a valid string representation of an integer (or is just an empty string). To work around that, use Int32.TryParse()
.
textBox1.Text
可能不包含整数的有效字符串表示形式(或者只是一个空字符串)。要解决这个问题,请使用Int32.TryParse()
.
回答by James Black
You can do:
你可以做:
Convert.ToInt32(input);
For a longer function using this you can look at: http://msdn.microsoft.com/en-us/library/bb397679.aspx
对于使用此功能的更长的功能,您可以查看:http: //msdn.microsoft.com/en-us/library/bb397679.aspx
Basically it checks if the string is null, then it will call int.Parse. This will work under WindowsCE also, which doesn't have int.TryParse.
基本上它检查字符串是否为空,然后它会调用 int.Parse。这也适用于没有 int.TryParse 的 WindowsCE。
回答by samjudson
To be specific as to why your code fails to compile it is because you are comparing a string (newItem) against the result of Convert.ToInt32, which is an integer, which it wont let you do. Also Convert.ToInt32 will raise an exception it the string passed in is not a number.
具体到为什么您的代码无法编译它是因为您正在将一个字符串 (newItem) 与 Convert.ToInt32 的结果进行比较,这是一个整数,它不会让您这样做。如果传入的字符串不是数字,Convert.ToInt32 也会引发异常。
You can try using int.TryParse
, or alternatively write a simple regular expression to validate your input:
您可以尝试使用int.TryParse
,或者编写一个简单的正则表达式来验证您的输入:
int i;
bool isInteger = int.TryParse(textBox1.Text,out i);
or
或者
bool isInteger = System.Text.RegularExpressions.Regex.IsMatch("^\d+$", textBox1.Text);