C# UInt32 TryParse(String, NumberStyles, IFormatProvider, UInt32)

Description

UInt32 TryParse(String, NumberStyles, IFormatProvider, UInt32) tries to convert the string representation of a number in a specified style and culture-specific format to its 32-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed.

Syntax

UInt32.TryParse(String, NumberStyles, IFormatProvider, UInt32) has the following syntax.


[CLSCompliantAttribute(false)]//from  w  ww  .  j  a va 2s  .  c o  m
public static bool TryParse(
  string s,
  NumberStyles style,
  IFormatProvider provider,
  out uint result
)

Parameters

UInt32.TryParse(String, NumberStyles, IFormatProvider, UInt32) has the following parameters.

  • s - A string that represents the number to convert. The string is interpreted by using the style specified by the style parameter.
  • style - A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is NumberStyles.Integer.
  • provider - An object that supplies culture-specific formatting information about s.
  • result - When this method returns, contains the 32-bit unsigned integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not in a format compliant with style, or represents a number that is less than UInt32.MinValue or greater than UInt32.MaxValue. This parameter is passed uninitialized.

Returns

UInt32.TryParse(String, NumberStyles, IFormatProvider, UInt32) method returns true if s was converted successfully; otherwise, false.

Example

The following example calls the TryParse(String, NumberStyles, IFormatProvider, UInt32) method with a number of different strings and NumberStyles values.


/*  w  w  w  .j  ava  2s  .c  o  m*/
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;

      numericString = "1234567";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);

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

      numericString = "12345678.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "12345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "12345678E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 

      numericString = "1234E-01";
      CallTryParse(numericString, styles);

      numericString = "123E01";
      CallTryParse(numericString, styles); 

      numericString = "FFC86DA1";
      CallTryParse(numericString, NumberStyles.HexNumber);

      numericString = "0x8F8C";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      uint number;
      bool result = UInt32.TryParse(stringToConvert, styles, 
                                   CultureInfo.InvariantCulture, out number);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.", 
                           Convert.ToString(stringToConvert));
   }
}

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