当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Uri.EscapeDataString(String)用法及代码示例


Uri.EscapeDataString(String)方法用于将字符串转换为其转义的表示形式。

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

返回值:此方法返回一个字符串,其中包含转义的stringToEscape表示形式。


异常:如果stringToEscape为null,则此方法引发ArgumentNullException。
UriFormatException如果长度stringToEscape超过32766个字符。

以下示例程序旨在说明Uri.EscapeDataString(String)方法的用法:

示例1:

// 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); 
        } 
    } 
}
输出:
Escaped string is : http%3A%2F%2Fwww.contoso.com%2Findex.htm%23search

示例2:对于ArgumentNullException

// 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); 
        } 
    } 
}
输出:
stringToEscape can not be null
Exception Thrown: System.ArgumentNullException

示例3:对于UriFormatException

// 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); 
        } 
    } 
}
输出:
Length of stringToEscape should not exceed from 32766 characters.
Exception Thrown: System.UriFormatException

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Uri.EscapeDataString(String) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。