Attempts to parse a string representing an integer. - CSharp System.Xml

CSharp examples for System.Xml:XML String

Description

Attempts to parse a string representing an integer.

Demo Code


using System.Xml.Linq;
using System.Globalization;
using System;//from w  ww  .  j a  v a 2  s.com

public class Main{
        /// <summary>
      /// Attempts to parse a string representing an integer.
      /// The input string may hold a number represented in either decimal or in hexadecimal.
      /// </summary>
      /// <param name="numberStr">The string to parse. If it begins with "0x", it will be parsed as hexadecimal.</param>
      /// <param name="result">The variable to store the parsed value to if parsing succeeds.</param>
      /// <returns>true if parsing the string succeeded.</returns>
      public static bool ParseNumber (string numberStr, out int result)
      {
         if ( numberStr.StartsWith("0x") )
            return int.TryParse(numberStr.Substring(2), NumberStyles.HexNumber, null, out result);
         return int.TryParse(numberStr, out result);
      }
}

Related Tutorials