Q: How to resolve a string to an int?
A: There are three simple methods:
String source = "1412 ";
Int result = 0;
// Use convert. toint32 (string value );
Result = convert. toint32 (source );
// Use int32.parse (string value );
Result = int32.parse (source );
// Use int32.tryparse (string S, out int result );
Int32.tryparse (source, out result );
Q: What are the differences between the three methods?
A: A simple answer is:
If the parsing fails, int32.parse (source) will always throw an exception; convert. toint32 (source) does not throw an exception when source is null, but simply returns 0 to the caller. int32.tryparse (source, result) does not throw an exception in any case, only true or false is returned to indicate whether the resolution is successful. If the resolution fails, the caller will get a value of 0.
Q: If the literal value of the string to be parsed is not in decimal format, the returned values obtained from these methods are incorrect. Is there any solution?
A: You need to reload the corresponding versions of these methods. A simple method is to use the convert class.
Public static int toint32 (string value, int frombase );
The frombase value can only be 2, 8, 10, or 16, which is used to specify the hexadecimal mode. If frombase is not a specified value or the value is not in decimal format and has a positive or negative prefix, argumentexception is thrown.
String source = "0x1412"; // The 0x (or 0x) prefix here is optional.
Int result = convert. toint32 (source, 16 );
Of course, you can also
Public static int parse (string S, numberstyles style );
Specify numberstyles. allowhexspecifier or numberstyles. hexnumber as the second parameter to parse the string of the hexadecimal literal value. In this case, you need to reference the system. Globalization namespace.
Or use the int32 class
Public static bool tryparse (string S, numberstyles style, iformatprovider provider, out int result );
Specify numberstyles. allowhexspecifier or numberstyles. hexnumber as the second parameter, and null as the third parameter to parse the string of the hexadecimal literal value. Of course you should also reference the system. Globalization namespace.
Note that no matter whether the parse or tryparse method is used to parse the hexadecimal format, the character string cannot contain the 0x or 0x prefix. Otherwise, an exception is thrown.
Q: What if I want to convert string represented by the scientific notation to int?
A: You can set numberstyles. allowdecimalpoint | numberstyles. allowexponent (bitwise operation of two nunberstyles enumeration values, the former indicates that there may be a decimal point, and the latter indicates that there may be an exponential symbol of scientific Notation) as the second parameter passed to int32 class
Public static int parse (string S, numberstyles style );
Or
Public static bool tryparse (string S, numberstyles style, iformatprovider provider, out int result );
If the parsed result is incompatible with the int, you need to store the result in another type. For example, "1.412e2" should store the parsing result in a float, double, or decimal type variable. Of course, you should also use the type method corresponding to the storage variable for parsing:
Double result = double. parse ("1.412e2", numberstyles. allowdecimalpoint | numberstyles. allowexponent );
The entire string expression should have no space. Otherwise, an exception may be thrown.
The scientific Note format is [{+ |-}] M. dddddd {e | e} [{+ |-}] XX, which can be divided into the following forms:
[-] M. dddddde + xx
[-] M. dddddde-xx
[-] M. dddddde + xx
[-] M. dddddde-xx
Which of the following statements cannot be parsed properly:
"1.412 E3"
"1.412e 3"
"1.412e + 3"
"142.16e-2"
Q: How do I deal with spaces contained in the string to be parsed?
A: you also have a variety of options for the string prefix or suffix space. Generally, you can directly use the string class
Public String trim ();
To take the spaces that may be contained at the end of the U-turn:
Int result = int32.parse (textbox1.text. Trim ());
Alternatively, you can use numberstyles. allowleadingwhite | numberstyles. allowtrailingwhite to tell parse or tryparse that the header and tail of the string to be parsed may contain spaces.
Int result = int32.parse (textbox1.text, numberstyles. allowleadingwhite | numberstyles. allowtrailingwhite );
If the string to be parsed is represented by scientific notation, you can
Int result = int32.parse ("1.412e3", numberstyles. Float );
Numberstyles. Float tells the parse method that the string to be parsed may contain spaces with the prefix or suffix, plus or minus signs with the prefix, (decimal point), and scientific notation index representation.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 342178