UInt32.Parse Method (String)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Converts the string representation of a number to its 32-bit unsigned integer equivalent.

This API is not CLS-compliant. The CLS-compliant alternative is Parse(String).

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

<CLSCompliantAttribute(False)> _
Public Shared Function Parse ( _
    s As String _
) As UInteger
[CLSCompliantAttribute(false)]
public static uint Parse(
    string s
)

Parameters

Return Value

Type: System..::.UInt32
A 32-bit unsigned integer equivalent to the number contained in s.

Exceptions

Exception Condition
ArgumentNullException

The s parameter is nullNothingnullptra null reference (Nothing in Visual Basic).

FormatException

The s parameter is not of the correct format.

OverflowException

The s parameter represents a number that is less than UInt32..::.MinValue or greater than UInt32..::.MaxValue.

Remarks

The s parameter should be the string representation of a number in the following form.

[ws][sign]digits[ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space.

sign

An optional sign. Valid sign characters are determined by the NumberFormatInfo..::.NegativeSign and NumberFormatInfo..::.PositiveSign properties of the current culture. However, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.

digits

A sequence of digits ranging from 0 to 9. Any leading zeros are ignored.

Note

The string specified by the s parameter is interpreted by using the NumberStyles..::.Integer style. It cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

The s parameter is parsed by using the formatting information in a System.Globalization..::.NumberFormatInfo object that is initialized for the current system culture. For more information, see NumberFormatInfo..::.CurrentInfo. To parse a string by using the formatting information of a specific culture, use the Parse(String, IFormatProvider) method.

Examples

The following example uses the Parse(String) method to parse an array of string values.

Dim values() As String = {"+13230", "-0", "1,390,146", "$190,235,421,127", _
                           "0xFA1B", "163042", "-10", "14065839182", _
                           "16e07", "134985.0", "-12034"}
For Each value As String In values
   Try
      Dim number As UInteger = UInt32.Parse(value)
      outputBlock.Text += String.Format("{0} --> {1}", value, number) & vbCrLf
   Catch e As FormatException
      outputBlock.Text += String.Format("{0}: Bad Format", value) & vbCrLf
   Catch e As OverflowException
      outputBlock.Text += String.Format("{0}: Overflow", value) & vbCrLf
   End Try
Next
' The example displays the following output:
'       +13230 --> 13230
'       -0 --> 0
'       1,390,146: Bad Format
'       $190,235,421,127: Bad Format
'       0xFA1B: Bad Format
'       163042 --> 163042
'       -10: Overflow
'       14065839182: Overflow
'       16e07: Bad Format
'       134985.0: Bad Format
'       -12034: Overflow
string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
                    "0xFA1B", "163042", "-10", "14065839182",
                    "16e07", "134985.0", "-12034" };
foreach (string value in values)
{
   try
   {
      uint number = UInt32.Parse(value);
      outputBlock.Text += String.Format("{0} --> {1}", value, number) + "\n";
   }
   catch (FormatException)
   {
      outputBlock.Text += String.Format("{0}: Bad Format", value) + "\n";
   }
   catch (OverflowException)
   {
      outputBlock.Text += String.Format("{0}: Overflow", value) + "\n";
   }
}
// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10: Overflow
//       14065839182: Overflow
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034: Overflow      

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

UInt32 Structure

Parse Overload

System Namespace

ToString