w3resource

C# Sharp Exercises: Check a string is Valid Hex Code or not

C# Sharp Regular Expression: Exercise-1 with Solution

Hex color codes start with a pound sign or hashtag (#) and are followed by six letters (A-F) and/or number (digit from 0-9). Alphabetic characters may be uppercase or lowercase.

Write a C# Sharp program to check whether a given string is a valid Hex code or not. Return true if the said string is a valid code otherwise false.

Sample Data:
("#CD5C5C") -> True
("#f08080") -> True
("#E9967A") -> True
("#EFFA07A") -> False

Sample Solution-1:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string hc = "#CD5C5C";
            Console.WriteLine("Hex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
            hc = "#f08080";
            Console.WriteLine("\nHex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
            hc = "#E9967A";
            Console.WriteLine("\nHex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
            hc = "#EFFA07A";
            Console.WriteLine("\nHex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
        }

        public static bool test(string hc)
         {

            return Regex.IsMatch(hc, @"[#][0-9A-Fa-f]{6}\b");
         }
    }
}

Sample Output:

Hex Code: #CD5C5C
Check the said string is valid hex code or not: True

Hex Code: #f08080
Check the said string is valid hex code or not: True

Hex Code: #E9967A
Check the said string is valid hex code or not: True

Hex Code: #EFFA07A
Check the said string is valid hex code or not: False

Flowchart:

Flowchart: C# Sharp Exercises - Check a string is Valid Hex Code or not

Sample Solution-2:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {

            string hc = "#CD5C5C";
            Console.WriteLine("Hex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
            hc = "#f08080";
            Console.WriteLine("\nHex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
            hc = "#E9967A";
            Console.WriteLine("\nHex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
            hc = "#EFFA07A";
            Console.WriteLine("\nHex Code: " + hc);
            Console.WriteLine("Check the said string is valid hex code or not: " + test(hc));
        }

        public static bool test(string hc)
        {
            return Regex.Match(hc, "^#[0-9a-fA-F]{6}$").Success;
        }
    }
}

Sample Output:

Hex Code: #CD5C5C
Check the said string is valid hex code or not: True

Hex Code: #f08080
Check the said string is valid hex code or not: True

Hex Code: #E9967A
Check the said string is valid hex code or not: True

Hex Code: #EFFA07A
Check the said string is valid hex code or not: False

Flowchart:

Flowchart: C# Sharp Exercises - Check a string is Valid Hex Code or not

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: C# Sharp Regular Expression Exercises.
Next C# Sharp Exercise: Calculate the average word length in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter