C# | Uri.EscapeDataString(String) Method

Uri.EscapeDataString(String) Method is used to convert a string to its escaped representation.

Syntax: public static string EscapeDataString (string stringToEscape);
Here, it takes the string to escape.

Return Value: This method returns a string which contains the escaped representation of stringToEscape.

Exception: This method throws ArgumentNullException if stringToEscape is null.
and UriFormatException if The length of stringToEscape exceeds 32766 characters.

Below programs illustrate the use of Uri.EscapeDataString(String) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Uri.EscapeDataString(String) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing address1
            string address1 = "http://www.contoso.com/index.htm#search";
  
            // Converting a string to its 
            // escaped representation
            // using EscapeDataString() method
            string value = Uri.EscapeDataString(address1);
  
            // Displaying the result
            Console.WriteLine("Escaped string is : {0}", value);
        }
  
        catch (ArgumentNullException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Escaped string is : http%3A%2F%2Fwww.contoso.com%2Findex.htm%23search

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Uri.EscapeDataString(String) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Declaring and initializing address1
            string address1 = null;
  
            // Converting a string to its 
            // escaped representation
            // using EscapeDataString() method
            string value = Uri.EscapeDataString(address1);
  
            // Displaying the result
            Console.WriteLine("Escaped string is : {0}", value);
        }
  
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("stringToEscape can not be null");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        catch (UriFormatException e) 
        {
            Console.WriteLine("Length of stringToEscape should"+
                          " not exceed from 32766 characters.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

stringToEscape can not be null
Exception Thrown: System.ArgumentNullException

Example 3: For UriFormatException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Uri.EscapeDataString(String) Method
using System;
using System.Text;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing address1
            StringBuilder address1 = new StringBuilder("http://www.contoso.com/index.htm#search");
  
            // appending StringBuilder
            for (int i = 1; i <= 3000; i++)
                address1.Append("abcedfghijklmnopdjdjdjdjdjjddjjdjdj");
  
            // Converting a string to its 
            // escaped representation
            // using EscapeDataString() method
            string value = Uri.EscapeDataString(address1.ToString());
  
            // Displaying the result
            Console.WriteLine("Escaped string is : {0}", value);
        }
  
        catch (ArgumentNullException e)
       {
            Console.WriteLine("stringToEscape can not be null");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        catch (UriFormatException e)
        {
            Console.WriteLine("Length of stringToEscape should"+
                          " not exceed from 32766 characters.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Length of stringToEscape should not exceed from 32766 characters.
Exception Thrown: System.UriFormatException

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.