當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。