C# UInt32 Parse(String, NumberStyles)

Description

UInt32 Parse(String, NumberStyles) converts the string representation of a number in a specified style to its 32-bit unsigned integer equivalent.

Syntax

UInt32.Parse(String, NumberStyles) has the following syntax.


[CLSCompliantAttribute(false)]/*  w w w.j a  va 2 s . co  m*/
public static uint Parse(
  string s,
  NumberStyles style
)

Parameters

UInt32.Parse(String, NumberStyles) has the following parameters.

  • s - A string representing the number to convert. The string is interpreted by using the style specified by the style parameter.
  • style - A bitwise combination of the enumeration values that specify the permitted format of s. A typical value to specify is Integer.

Returns

UInt32.Parse(String, NumberStyles) method returns A 32-bit unsigned integer equivalent to the number specified in s.

Example

The following example tries to parse each element in a string array by using a number of NumberStyles values.


using System;//from   w w  w .jav  a2  s  .co m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values= { " 223423 ", "1,234,234", "(0)", "12342+", " + 22349 ", 
                         " +22349 ", "123423.00", "1e03ff", "23423.0e-2" };
      NumberStyles whitespace =  NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
      NumberStyles[] styles= { NumberStyles.None, whitespace, 
                               NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace, 
                               NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol, 
                               NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };

      foreach (string value in values)
      {
         foreach (NumberStyles style in styles)
         {
            try {
               uint number = UInt32.Parse(value, style);
               Console.WriteLine("   {0}: {1}", style, number);
            }   
            catch (FormatException) {
               Console.WriteLine("   {0}: Bad Format", style);
            }   
            catch (OverflowException)
            {
               Console.WriteLine("   {0}: Overflow", value);         
            }         
         }
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version