Converts string in a specified style and culture-specific format to 32-bit signed integer : Convert from String « Data Types « VB.Net






Converts string in a specified style and culture-specific format to 32-bit signed integer

 

Imports System.Globalization

Module StringParsing
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles

      numericString = "1"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)

      numericString = "-3"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)

      styles = NumberStyles.AllowLeadingSign
      CallTryParse(numericString, styles)

      numericString = "3-"
      CallTryParse(numericString, styles)

      styles = styles Or NumberStyles.AllowTrailingSign
      CallTryParse(numericString, styles)

      numericString = "$1"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)

      styles = NumberStyles.Integer Or NumberStyles.AllowCurrencySymbol
      CallTryParse(numericString, styles)

      numericString = "1.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "1.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "2,593" 
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(numericString, styles)

      numericString = "1E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 

      numericString = "1E03"
      CallTryParse(numericString, styles) 

      numericString = "8c1"
      CallTryParse(numericString, NumberStyles.HexNumber)

      numericString = "0x80C1"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub

   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As Integer
      Dim provider As CultureInfo

      Dim result As Boolean = Int32.TryParse(stringToConvert, styles,provider, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.",Convert.ToString(stringToConvert))
      End If                                                                           
   End Sub
End Module

   
  








Related examples in the same category

1.DateTime TryParse: Assume current culture is en-US, and dates of the form: MMDDYYYY
2.TryParseExact: Use fr-FR culture.
3.Boolean TryParse
4.Char TryParse
5.Byte TryParse
6.Int16 TryParse
7.Int32 TryParse
8.Int64 TryParse
9.Decimal TryParse
10.Single TryParse
11.Double TryParse
12.Use the simple Double.TryParse overload, but specify an invalid value
13.SByte TryParse
14.UInt16 TryParse
15.UInt32 TryParse
16.UInt64 TryParse
17.Try to Parse DateTime by using different cultures
18.Parse value with trailing sign
19.Parse a string in exponential notation with only the AllowExponent flag
20.Parse a string in exponential notation with the AllowExponent and Number flags.
21.Parse a currency value with leading and trailing white space, and white space after the U.S. currency symbol.
22.Parse negative value with thousands separator and decimal.
23.Converts the numeric value of this instance to its equivalent string representation.
24.Parse a floating-point value with a currency symbol and a thousands separator.
25.Parse value in exponential notation.
26.Parse a negative integer number.
27.Parse string using "$" as the currency symbol for en-GB and en-us cultures
28.Parse a floating-point value with a thousands separator
29.Parse a floating-point value with a currency symbol and a thousands separator
30.Parse value in exponential notation
31.Parse currency value using en-GB culture
32.Parse String to Integer
33.Parse Hex String to Integer
34.Parse string to Integer
35.Parse Hex Number string to Integer
36.Converts string in a specified style and culture-specific format to its 32-bit signed integer equivalent.
37.Converts string to 32-bit signed integer equivalent