在 C# 中检查字符串并转换为 int 的最佳方法
Best way to check string and convert to int in c#
请帮助我改进我的代码。
这个想法是:
如果字符串没问题
然后转换为 int
1- 它只检查 null 或空白字符串
int t=0;
if(!string.IsNullOrEmpty(textbox1.text.trim())
t= int.Parse(textbox1.text.trim());
2-
if(int.tryparse(textbox1.text.trim(), out t)
t=int.Parse(textbox1.text.trim());
或简称
return string.IsNullOrEmpty(textbox1.text.trim()) ? 0 : int.Parse(textbox1.text.trim());
还有其他更好的方法吗?
int t = 0;
int.TryParse(textbox1?.Text?.Trim(), out t);
获取用户输入并将其转换为整数的正确方法是通过 Int32.TryParse 方法。如果输入错误(如 Parse 或 Convert.ToInt32),此方法的优点是不会引发代价高昂的异常,但 returns true 或 false 允许您向用户显示有意义的错误消息。
int t;
if(Int32.TryParse(textbox1.Text, out t)
{
// t has ben set with the integer converted
// add here the code that uses the t variable
}
else
{
// textbox1.Text doesn't contain a valid integer
// Add here a message to your users about the wrong input....
// (if needed)
}
请注意,textbox1.Text 永远不会为 null,因此您无需明确检查它。当然,我假设此 textbox1 是在您的 InitializeComponent 调用中定义的 TextBox 控件,因此它本身不是 null。
int i = 0;
Int32.TryParse(TextBox1.Text, out i);
是的。我们需要检查 TryParse return 是否为真。如果为 true,则成功;如果有任何错误,则为 false occurs.The 如果 TryParse 失败或实际字符串值为 0,则 TryParse 将为 return 0。
string s2 = "13.3";
int i;
//i = Convert.ToInt32(s2); // Run Time Error
Console.WriteLine(int.TryParse(s2, out i)); // False
Console.WriteLine(i); // Output will be 0
string s3 = "Hello";
//i = Convert.ToInt32(s2); // Run Time Error
Console.WriteLine(int.TryParse(s3, out i)); // False
Console.WriteLine(i); // Output will be 0
string s1 = null;
Console.WriteLine(int.TryParse(s1, out i)); // False
Console.WriteLine(i); // Output will be 0
string s4 = "0";
Console.WriteLine(int.TryParse(s4, out i)); // return True
Console.WriteLine(i); // Output will be 0
请帮助我改进我的代码。 这个想法是: 如果字符串没问题 然后转换为 int
1- 它只检查 null 或空白字符串
int t=0;
if(!string.IsNullOrEmpty(textbox1.text.trim())
t= int.Parse(textbox1.text.trim());
2-
if(int.tryparse(textbox1.text.trim(), out t)
t=int.Parse(textbox1.text.trim());
或简称
return string.IsNullOrEmpty(textbox1.text.trim()) ? 0 : int.Parse(textbox1.text.trim());
还有其他更好的方法吗?
int t = 0;
int.TryParse(textbox1?.Text?.Trim(), out t);
获取用户输入并将其转换为整数的正确方法是通过 Int32.TryParse 方法。如果输入错误(如 Parse 或 Convert.ToInt32),此方法的优点是不会引发代价高昂的异常,但 returns true 或 false 允许您向用户显示有意义的错误消息。
int t;
if(Int32.TryParse(textbox1.Text, out t)
{
// t has ben set with the integer converted
// add here the code that uses the t variable
}
else
{
// textbox1.Text doesn't contain a valid integer
// Add here a message to your users about the wrong input....
// (if needed)
}
请注意,textbox1.Text 永远不会为 null,因此您无需明确检查它。当然,我假设此 textbox1 是在您的 InitializeComponent 调用中定义的 TextBox 控件,因此它本身不是 null。
int i = 0;
Int32.TryParse(TextBox1.Text, out i);
是的。我们需要检查 TryParse return 是否为真。如果为 true,则成功;如果有任何错误,则为 false occurs.The 如果 TryParse 失败或实际字符串值为 0,则 TryParse 将为 return 0。
string s2 = "13.3";
int i;
//i = Convert.ToInt32(s2); // Run Time Error
Console.WriteLine(int.TryParse(s2, out i)); // False
Console.WriteLine(i); // Output will be 0
string s3 = "Hello";
//i = Convert.ToInt32(s2); // Run Time Error
Console.WriteLine(int.TryParse(s3, out i)); // False
Console.WriteLine(i); // Output will be 0
string s1 = null;
Console.WriteLine(int.TryParse(s1, out i)); // False
Console.WriteLine(i); // Output will be 0
string s4 = "0";
Console.WriteLine(int.TryParse(s4, out i)); // return True
Console.WriteLine(i); // Output will be 0