How to resolve String to int?

xiaoxiao2021-03-05  48

Q: How to resolve String to int?

A: Simple method has three types:

String Source = "1412";

Int results = 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 is the difference between these three methods?

A: A simple answer is:

If the resolution fails, int32.parse will always throw an exception; convert.Toint32 (Source) does not throw an exception in the case of null, but simply returns 0 to the transfer party; but int32.tryparse (Source , RESULT, no matter how unusual, only returns True or False to clear whether the resolution is successful, if the resolution fails, the call party will get 0 value.

Q: If the literal value of the string I want to parse is not a decimal, there is a problem with the return value obtained from these methods. Is there any way to solve?

A: So you need these methods for the corresponding overload version, a simple method is to use the Convert class

Public Static int toint32 (string value, int.com);

Where the value of fromBASE can only be 2,8, 10 or 16 for specifying the way. If the FROSE is not a specified value or Value is not decimal, the argumentException will be thrown.

String Source = "0x1412"; // The 0x (or 0x) prefix here is optional.

int result = convert.toint32 (source, 16);

Of course, you can also pass the Int32 class

Public Static Int PaSe (String S, NumberStyles Style);

Specify NumBerstyles.allowHexSpecifier or NumBerstyles.HexNumber to parse the strings of the sixteent-string value for the second parameter. At this time, you need to reference the System.globalization namespace.

Or use int32 class

Public Static Bool TryParse (String S, NumberStyles Style, IFORMATPROVIDER Provider, Out Int Result)

And specify NumBerstyles.allowhexSpecifier or NumBerstyles.HexNumber as the second parameter, null is the third parameter to parse the string of the hexadecimal value. You must also reference the System.Globalization namespace.

One thing to be reminded here is that in writing, whether to use the PARSE or TryParse method, the string cannot appear 0x or 0x prefix, otherwise an exception will be thrown.

Q: If I want to convert the String use of the scientific number of scientific numbers to INT?

A: You can use NumberStyles.allowDecimalPoint | NumBerstyles.allowPonnet (to operate two NUNBERSTYLES enumerated, where the former will exist in a decimal point, and the latter explains the index symbols that may have scientific counting as the second parameter. Public Static Int PaSe (String S, NumberStyles Style) passed to INT32 class;

or

Public Static Bool TryParse (String S, NumberStyles Style, IFORMATPROVIDER Provider, Out Int Result)

If the resulting results are incompatible with int, consider storing the results in other types. For example, "1.412E2" should store the resolution result in a variable of FLOAT or DOUBLE or DECIMAL type, of course, you should also be parsed by methods of the type corresponding to the storage variable:

Double Result = double.parse ("1.412E2", NumberStyles.allowDecimalpoint | NumberStyles.allowPonent;

The entire string expression should not have any spaces, otherwise it will throw an exception.

The format of scientific counting is [{ | -}] m.ddddddd {e | e} [{ | -}] xx, can be broken down into the following forms:

[-] M.DDDDDE XX

[-] M.DDDDDE-XX

[-] M.DDDDDE XX

[-] M.DDDDDE-XX

The following listings do not correctly parse:

"1.412 E3"

"1.412e 3"

"1.412e 3"

"141200E- 2"

Q: How to deal with the spaces contained in the String in String?

A: For the prefix or suffix space of the string, you also have a variety of options, in general, you can use the String class directly.

Public String Trim ();

To take the space that may contain the head:

Int result = int32.parse (TextBox1.Text.trim ());

Alternatively, you use NumBerstyles.allowleadingWhite | NumberStyles.allowTrailingWhite to tell Parse or tryparse's heads that may contain spaces.

Int result = int32.parse (TextBox1.Text, NumberStyles.allowleadingWhite | NumberStyles.Allowtrailingwhite);

If the string to be parsed is represented by the scientific countd, then you can

Int results = int32.Parse ("1.412e3", NumberStyles.float;

Where numberstyles.float tells the Parse method to be parsed, the string may prefix or suffix the space, the prefix is ​​negative, (decimal) decimal point, the scientific counting method exponentially.

转载请注明原文地址:https://www.9cbs.com/read-34581.html

New Post(0)