C# | Uri.IsHexEncoding() Method

Uri.IsHexEncoding(String, Int32) Method is used to check whether a character in a string is hexadecimal encoded or not. This checks for hexadecimal encoding which follows the pattern “%hexhex” in a string, where “hex” is a digit from 0 to 9 or a letter from A-F (case-insensitive).

Syntax: public static bool IsHexEncoding (string pattern, int index);

Parameters:
pattern: It is the string to check.
index: It is the location in pattern to check for hexadecimal encoding.

Return Value: This method returns a Boolean value that is true if pattern is hexadecimal encoded at the specified location otherwise, false.

Below programs illustrate the use of Uri.IsHexEncoding(String, Int32) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Uri.IsHexEncoding(String,
// Int32)  Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and initializing pattern
        string pattern = "%75";
  
        // Declaring and initializing index
        int index = 1;
  
        // Validating the Character in the String
        // using IsHexEncoding(String, Int32)  method
        bool value = Uri.IsHexEncoding(pattern, index);
  
        // Displaying the result
        if (value)
            Console.WriteLine("{0}({1}) is a valid "+
                     "Hexadecimal Encoded", pattern);
        else
            Console.WriteLine("{0} is a valid"+
              " Hexadecimal Encoded", pattern);
    }
}

chevron_right


Output:

%75 is a valid Hexadecimal Encoded

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Uri.IsHexEncoding(String, 
// Int32) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // calling get() method
        get("%65", 1);
        get("%75", 0);
        get("%55", 0);
    }
  
    // defining get() method
    public static void get(string pattern, 
                                int index)
    {
  
        // Validating the Character in the String
        // using IsHexEncoding(String, Int32)  method
        bool value = Uri.IsHexEncoding(pattern, index);
  
        // Displaying the result
        if (value)
            Console.WriteLine("{0} is a valid"+
              " Hexadecimal Encoded", pattern);
        else
            Console.WriteLine("{0} is a not valid"+
                  " Hexadecimal Encoded", pattern);
    }
}

chevron_right


Output:

%65 is a not valid Hexadecimal Encoded
%75 is a valid Hexadecimal Encoded
%55 is a valid Hexadecimal Encoded

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.