語言

Uri.GetLeftPart(UriPartial) 方法

定義

取得實例中指定的部分 Uri 。

public:
 System::String ^ GetLeftPart(UriPartial part);
public string GetLeftPart(UriPartial part);
member this.GetLeftPart : UriPartial -> string
Public Function GetLeftPart (part As UriPartial) As String

參數

part
UriPartial

其中一個列舉值,指定要回傳 URI 部分的結尾。

傳回

實例中指定的部分 Uri 。

例外狀況

目前 Uri 的實例並非絕對實例。

但規定 part 是不成立的。

範例

以下範例建立一個 Uri 實例並寫入通往主控台的路徑。

// Create Uri
Uri uriAddress = new Uri("http://www.contoso.com/index.htm#search");
Console.WriteLine(uriAddress.Fragment);
Console.WriteLine("Uri {0} the default port ", uriAddress.IsDefaultPort ? "uses" : "does not use");

Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Path));
Console.WriteLine("Hash code {0}", uriAddress.GetHashCode());
// The example displays output similar to the following:
//        #search
//        Uri uses the default port
//        The path of this Uri is http://www.contoso.com/index.htm
//        Hash code -988419291
// Create Uri
let uriAddress = Uri "http://www.contoso.com/index.htm#search"
printfn $"{uriAddress.Fragment}"
printfn $"""Uri {if uriAddress.IsDefaultPort then "uses" else "does not use"} the default port """

printfn $"The path of this Uri is {uriAddress.GetLeftPart UriPartial.Path}"
printfn $"Hash code {uriAddress.GetHashCode()}"
// The example displays output similar to the following:
//        #search
//        Uri uses the default port
//        The path of this Uri is http://www.contoso.com/index.htm
//        Hash code -988419291
' Create Uri
Dim uriAddress As New Uri("http://www.contoso.com/index.htm#search")
Console.WriteLine(uriAddress.Fragment)
Console.WriteLine("Uri {0} the default port ", If(uriAddress.IsDefaultPort, "uses", "does not use")) 

Console.WriteLine("The path of this Uri is {0}", uriAddress.GetLeftPart(UriPartial.Path))
Console.WriteLine("Hash code {0}", uriAddress.GetHashCode())
' The example displays output similar to the following:
'        #search
'        Uri uses the default port
'        The path of this Uri is http://www.contoso.com/index.htm
'        Hash code -988419291

備註

此 GetLeftPart 方法回傳包含 URI 字串最左端部分的字串,結尾為 part。

Important

此 GetLeftPart 方法在處理過程中執行 Unicode 字元編碼與正規化。 它不是一種簡單的字串操作方法。 由於這種編碼行為,回傳的字串可能與原始 URI 字串不同。

此GetLeftPart方法等同於使用相應GetComponents旗標呼叫UriComponents。 例如:

  • GetLeftPart(UriPartial.Authority) 相當於 GetComponents(UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriComponents.Port, UriFormat.UriEscaped)

GetLeftPart 以下情況下包含分隔符:

  • Scheme 包含方案分隔符。
  • Authority 不包含路徑分隔符。
  • Path 包含路徑分隔符以及原始 URI 中查詢或片段分隔符中的任何分隔符。
  • Query 包含 Path,加上查詢及其分隔符。

以下範例展示了一個 URI 以及呼叫 GetLeftPart 、 SchemeAuthority、 Path或 Query的結果。

URI Scheme 權威 路徑 Query
http://www.contoso.com/index.htm?date=today http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm?date=today
http://www.contoso.com/index.htm#main http:// http://www.contoso.com http://www.contoso.com/index.htm http://www.contoso.com/index.htm
mailto:user@contoso.com?subject=uri mailto: <none> mailto:user@contoso.com mailto:user@contoso.com?subject=uri
nntp://news.contoso.com/123456@contoso.com nntp:// nntp://news.contoso.com nntp://news.contoso.com/123456@contoso.com nntp://news.contoso.com/123456@contoso.com
news:123456@contoso.com news: <none> news:123456@contoso.com news:123456@contoso.com
file://server/filename.ext file:// file://server file://server/filename.ext file://server/filename.ext

適用於