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


C# Uri.ToString()用法及代碼示例


Uri.HexEscape(Char)方法用於獲取指定Uri實例的規範字符串表示形式。

用法: public override string ToString ();

返回值:此方法返回一個String實例,其中包含Uri實例的未轉義規範表示形式。除#、?和%外,所有字符均未轉義。


以下示例程序旨在說明Uri.ToString()方法的使用:

示例1:

// C# program to demonstrate the 
// Uri.ToString() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        // Create a new Uri from a string address. 
        Uri uri = new Uri("HTTP://www.Contoso.com:80/thick%20and%20thin.htm"); 
  
        // Converts a specified uri into 
        // its string equivalent. 
        // using ToString() method 
        string value = uri.ToString(); 
  
        // Displaying the result 
        Console.WriteLine("Converted string is: {0}", value); 
    } 
}
輸出:
Converted string is: http://www.contoso.com/thick and thin.htm

示例2:

// C# program to demonstrate the 
// Uri.ToString() Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // calling get() method 
        get(new Uri("http://www.contoso.com")); 
        get(new Uri("http://www.google.com")); 
    } 
  
    // defining get() method 
    public static void get(Uri uri) 
    { 
  
        // Converts a specified uri  
        // into its string equivalent. 
        // using ToString() method 
        string value = uri.ToString(); 
  
        // Displaying the result 
        Console.WriteLine("Converted string is: {0}", value); 
    } 
}
輸出:
Converted string is: http://www.contoso.com/
Converted string is: http://www.google.com/

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Uri.ToString() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。