Jan 24, 2019 int is the Python standard built-in function to convert a string into an integer value. You call it with a string containing a number as the argument, and it returns the number converted to an integer: print (int('1') + 1) The above prints 2.
-->You can convert a string to a number by calling the
Parse
or TryParse
method found on the various numeric types (int
, long
, double
, etc.), or by using methods in the System.Convert class.If you have a string, it is slightly more efficient and straightforward to call a
TryParse
method (for example, int.TryParse('11', out number)
) or Parse
method (for example, var number = int.Parse('11')
). Using a Convert method is more useful for general objects that implement IConvertible.You can use
Parse
or TryParse
methods on the numeric type you expect the string contains, such as the System.Int32 type. The Convert.ToInt32 method uses Parse internally. The Parse
method returns the converted number; the TryParse
method returns a Boolean value that indicates whether the conversion succeeded, and returns the converted number in an out
parameter. If the string is not in a valid format, Parse
throws an exception, whereas TryParse
returns false. When calling a Parse
method, you should always use exception handling to catch a FormatException in the event that the parse operation fails.Calling the Parse and TryParse methods
The
Parse
and TryParse
methods ignore white space at the beginning and at the end of the string, but all other characters must be characters that form the appropriate numeric type (int
, long
, ulong
, float
, decimal
, etc.). Any white space within the string that forms the number causes an error. For example, you can use decimal.TryParse
to parse '10', '10.3', or ' 10 ', but you cannot use this method to parse 10 from '10X', '1 0' (note the embedded space), '10 .3' (note the embedded space), '10e1' (float.TryParse
works here), and so on. In addition, a string whose value is null
or String.Empty fails to parse successfully. You can check for a null or empty string before attempting to parse it by calling the String.IsNullOrEmpty method.The following example demonstrates both successful and unsuccessful calls to
Parse
and TryParse
.The following example illustrates one a approach to parsing a string that is expected to include leading numeric characters (including hexadecimal characters) and trailing non-numeric characters. It assigns valid characters from the beginning of a string to a new string before calling the TryParse method. Because the strings to be parsed contain a small number of characters, the example calls the String.Concat method to assign valid characters to a new string. For a larger string, the StringBuilder class can be used instead.
Calling the Convert methods
![Convert String To Int Convert String To Int](http://dbadiaries.com/wp-content/uploads/2017/11/sql-convert-string-to-int.jpg)
![Convert String To Int Convert String To Int](/uploads/1/2/5/8/125832278/759456274.jpg)
The following table lists some of the methods from the Convert class that you can use to convert a string to a number.
Numeric Type | Method |
---|---|
decimal | ToDecimal(String) |
float | ToSingle(String) |
double | ToDouble(String) |
short | ToInt16(String) |
int | ToInt32(String) |
long | ToInt64(String) |
ushort | ToUInt16(String) |
uint | ToUInt32(String) |
ulong | ToUInt64(String) |
The following example calls the Convert.ToInt32(String) method to convert an input string to an int. The example catches the two most common exceptions that can be thrown by this method, FormatException and OverflowException. If the resulting number can be incremented without exceeding Int32.MaxValue, the example adds 1 to the result and displays the output.