C# | Uri.EscapeDataString(String) Method Last Updated : 01 May, 2019 Comments Improve Suggest changes Like Article Like Report 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: csharp // 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); } } } Output: Escaped string is : https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttp%2Fwww.contoso.com%2Findex.htm%23search Example 2: For ArgumentNullException csharp // 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); } } } Output: stringToEscape can not be null Exception Thrown: System.ArgumentNullException Example 3: For UriFormatException csharp // 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); } } } Output: Length of stringToEscape should not exceed from 32766 characters. Exception Thrown: System.UriFormatException Reference: https://docs.microsoft.com/en-us/dotnet/api/system.uri.escapedatastring?view=netstandard-2.1 Comment More infoAdvertise with us Next Article C# | Uri.EscapeDataString(String) Method R rohitprasad3 Follow Improve Article Tags : C# CSharp-method CSharp-Uri-Class Similar Reads C# | Uri.ToString() Method Uri.ToString( ) Method is used to get a canonical string representation for the specified Uri instance. Syntax: public override string ToString (); Return Value: This method returns a String instance that contains the unescaped canonical representation of the Uri instance. All characters are unescap 2 min read C# | Char.ToString() Method In C#, Char.ToString() is a System.Char struct method which is used to convert the value of this instance to its equivalent string representation. This method can be overloaded by passing different type of arguments to it. Char.ToString(IFormatProvider) Method Char.ToString(Char) Method Char.ToStrin 2 min read C# | Uri.MakeRelativeUri(Uri) Method Uri.MakeRelativeUri(Uri) Method is used to determine the difference between two Uri instances. Syntax: public Uri MakeRelativeUri (Uri uri); Here, it takes the URI to compare to the current URI. Return Value: If the hostname and scheme of this URI instance and uri are the same, then this method retu 3 min read C# | Uri.IsWellFormedOriginalString() Method Uri.IsWellFormedOriginalString() Method is used to show whether the string used to construct this Uri was well-formed and is not required to be further escaped. Syntax: public bool IsWellFormedOriginalString (); Return Value: This method returns true if the string was well-formed otherwise, false. B 2 min read 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 sta 2 min read Like