📜  C#| Uri.ToString()方法

📅  最后修改于: 2021-05-29 13:34:57             🧑  作者: Mango

Uri.HexEscape(Char)方法用于获取指定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/

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.uri.tostring?view=netstandard-2.1