英語で読む

次の方法で共有


Uri.ToString メソッド

定義

指定した Uri インスタンスの正規文字列表現を取得します。

C#
public override string ToString ();

戻り値

String

エスケープ解除された正規形式の Uri インスタンス。 #、?、および % 以外のすべての文字は、エスケープ解除されます。

次の例では、文字列から新しい Uri インスタンスを作成します。 コンストラクターに渡された OriginalString文字列を返す値と、文字列の正規形式を返す呼び出し ToStringから返される値の違いを示します。

C#
// Create a new Uri from a string address.
Uri uriAddress = new Uri("HTTP://www.Contoso.com:80/thick%20and%20thin.htm");

// Write the new Uri to the console and note the difference in the two values.
// ToString() gives the canonical version.  OriginalString gives the orginal
// string that was passed to the constructor.

// The following outputs "http://www.contoso.com/thick and thin.htm".
Console.WriteLine(uriAddress.ToString());

// The following outputs "HTTP://www.Contoso.com:80/thick%20and%20thin.htm".
Console.WriteLine(uriAddress.OriginalString);

注釈

このメソッドによって返される文字列には、ポートがスキームの既定のポートである場合、ポート情報は含まれません。

注意

メソッドによって ToString 返される文字列に制御文字が含まれている場合があり、コンソール アプリケーションの状態が破損する可能性があります。 このメソッドを GetComponents 形式と共に UriFormat.SafeUnescaped 使用して、返された文字列から制御文字を削除できます。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0