Validates the char is 0-9, A-F or a-f - CSharp System

CSharp examples for System:Char

Description

Validates the char is 0-9, A-F or a-f

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   w w w . j a va  2s  . com

public class Main{
        /// <summary>
		/// Validates the char is 0-9, A-F or a-f
		/// </summary>
		public static bool IsHex(this char c)
		{
			if (char.IsDigit(c))
			{
				return true;
			}

			return char.ToUpperInvariant(c) >= 'A' && char.ToUpperInvariant(c) <= 'F';
		}
        /// <summary>
		/// Validates all chars are 0-9, A-F or a-f
		/// </summary>
		public static bool IsHex(this string str)
		{
			if (string.IsNullOrWhiteSpace(str))
			{
				return false;
			}

			return str.All(IsHex);
		}
}

Related Tutorials