[英]Converting string to int (or something else), is there a prefered way?
Sometimes i wonder, when converting strings to other types, for example int32, is one way better than the other, or is it just a matter of taste? 有时我想知道,在将字符串转换为其他类型时,例如int32,比另一种更好,或者只是品味问题?
Convert.ToInt32("123")
or 要么
Int32.Parse("123")
or 要么
Some other way? 还有其他方法吗?
Not considering the case if it's not a valid int in this question. 如果在这个问题中它不是有效的int,则不考虑这种情况。
The Convert.ToInt32 is actually implemented the following way... Convert.ToInt32实际上是通过以下方式实现的......
int.Parse(value, CultureInfo.CurrentCulture);
...which is the same as your stated alternative except it takes into account the culture settings. ...除了考虑到文化背景外,它与您声明的替代方案相同。 The int.Parse method is itself implemented the following way...
int.Parse方法本身通过以下方式实现...
Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
...where Number is an internal class that you cannot call directly. ...其中Number是您无法直接调用的内部类。 The Number.ParseInt32 method is marked with the following attribute...
Number.ParseInt32方法标有以下属性...
[MethodImpl(MethodImplOptions.InternalCall)]
...showing that it is implemented inside the CLR itself. ...显示它是在CLR本身内部实现的。
Main difference between Conver.ToInt32
and Int32.Parse
is how the treat null
strings. Conver.ToInt32
和Int32.Parse
之间的主要区别在于如何处理null
字符串。 Convert.ToInt32
returns default value in this case: 在这种情况下,
Convert.ToInt32
返回默认值:
public static int ToInt32(string value)
{
if (value == null)
return 0;
return Int32.Parse(value, CultureInfo.CurrentCulture);
}
I don't like that. 我不喜欢那样。 I think only
"0"
should be parsed to 0
. 我认为只应将
"0"
解析为0
。 This behavior initially was designed for Visual Basic programmers : 此行为最初是为Visual Basic程序员设计的 :
This is a set of conversion methods for programmers migrating from Visual Basic 6 to Visual Basic .NET that mirrored the behavior of the existing Visual Basic 6 conversion methods.
这是一组程序员从Visual Basic 6迁移到Visual Basic .NET的转换方法,它们反映了现有Visual Basic 6转换方法的行为。 The assumption was that C# programmers would be more comfortable with casting operators, whereas Visual Basic had traditionally used conversion methods for type conversion.
假设C#程序员更适合使用转换操作符,而Visual Basic传统上使用转换方法进行类型转换。
So, as non-VB programmer, I'd go with Int32.Parse
and Int32.TryParse
. 所以,作为非VB程序员,我会选择
Int32.Parse
和Int32.TryParse
。
When converting from string to int I always use int.TryParse. 当从string转换为int时,我总是使用int.TryParse。 Because you are not always sure you can convert a string to an int.
因为您并不总是确定可以将字符串转换为int。 Like this:
像这样:
string temp="222";
int intValue;
if(int.TryParse(temp,out intValue))
{
//Something
}
As V4Vendetta suggested it is better to use TryParse to avoid exceptions related to converting. 正如V4Vendetta建议的那样,最好使用TryParse来避免与转换相关的异常。
int result = 0;
a = "3";
string b = "b";
string c = "2147483648"; //int32 max value + 1
int.TryParse(a, out result); // returns true result=3
int.TryParse(b, out result); // returns false result=0
int.TryParse(c, out result); // returns false result=0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.