HttpUtility.HtmlDecode 方法

定義

將 HTTP 傳輸的 HTTP 編碼字串轉換成已解碼的字串。Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

多載

HtmlDecode(String)

將 HTTP 傳輸的 HTTP 編碼字串轉換成已解碼的字串。Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

HtmlDecode(String, TextWriter)

將已 HTML 編碼的字串轉換成已解碼的字串,並將解碼的字串傳送至 TextWriter 輸出資料流。Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream.

HtmlDecode(String)

將 HTTP 傳輸的 HTTP 編碼字串轉換成已解碼的字串。Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

public:
 static System::String ^ HtmlDecode(System::String ^ s);
public static string? HtmlDecode (string? s);
public static string HtmlDecode (string s);
static member HtmlDecode : string -> string
Public Shared Function HtmlDecode (s As String) As String

參數

s
String

要解碼的字串。The string to decode.

傳回

String

已解碼的字串。A decoded string.

範例

下列程式碼範例示範 HtmlEncode 類別的和 HtmlDecode 方法 HttpUtilityThe following code example demonstrates the HtmlEncode and HtmlDecode methods of the HttpUtility class. 輸入字串會使用方法進行編碼 HtmlEncodeThe input string is encoded using the HtmlEncode method. 接著會使用方法來解碼取得的編碼字串 HtmlDecodeThe encoded string obtained is then decoded using the HtmlDecode method.

using System;
using System.Web;
using System.IO;

class MyNewClass
{
    public static void Main()
    {
        Console.WriteLine("Enter a string having '&', '<', '>' or '\"' in it: ");
        string myString = Console.ReadLine();

        // Encode the string.
        string myEncodedString = HttpUtility.HtmlEncode(myString);

        Console.WriteLine($"HTML Encoded string is: {myEncodedString}");
        StringWriter myWriter = new StringWriter();

        // Decode the encoded string.
        HttpUtility.HtmlDecode(myEncodedString, myWriter);

        string myDecodedString = myWriter.ToString();
        Console.Write($"Decoded string of the above encoded string is: {myDecodedString}");
    }
}
Imports System.Web
Imports System.IO

Class MyNewClass
   Public Shared Sub Main()
      Dim myString As String
      Console.WriteLine("Enter a string having '&' or '""'  in it: ")
      myString = Console.ReadLine()
      Dim myEncodedString As String
      ' Encode the string.
      myEncodedString = HttpUtility.HtmlEncode(myString)
      Console.WriteLine("HTML Encoded string is " + myEncodedString)
      Dim myWriter As New StringWriter()
      ' Decode the encoded string.
      HttpUtility.HtmlDecode(myEncodedString, myWriter)
      Console.Write("Decoded string of the above encoded string is " + myWriter.ToString())
   End Sub
End Class

備註

如果在 HTTP 資料流程中傳遞空白和標點符號之類的字元,它們可能會在接收端被誤解。If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. HTML 編碼會將 HTML 中不允許的字元轉換為對等的字元實體;HTML 解碼會反轉編碼。HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. 例如,內嵌在文字區塊中時,會將字元 < and > 編碼為 &lt;&gt; HTTP 傳輸。For example, when embedded in a block of text, the characters < and > are encoded as &lt; and &gt; for HTTP transmission.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

另請參閱

適用於

HtmlDecode(String, TextWriter)

將已 HTML 編碼的字串轉換成已解碼的字串,並將解碼的字串傳送至 TextWriter 輸出資料流。Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream.

public:
 static void HtmlDecode(System::String ^ s, System::IO::TextWriter ^ output);
public static void HtmlDecode (string? s, System.IO.TextWriter output);
public static void HtmlDecode (string s, System.IO.TextWriter output);
static member HtmlDecode : string * System.IO.TextWriter -> unit
Public Shared Sub HtmlDecode (s As String, output As TextWriter)

參數

s
String

要解碼的字串。The string to decode.

output
TextWriter

TextWriter 的輸出資料流。A TextWriter stream of output.

備註

如果在 HTTP 資料流程中傳遞空白和標點符號之類的字元,它們可能會在接收端被誤解。If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. HTML 編碼會將 HTML 中不允許的字元轉換為對等的字元實體;HTML 解碼會反轉編碼。HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. 例如,內嵌在文字區塊中時,會將字元 < and > 編碼為 &lt;&gt; HTTP 傳輸。For example, when embedded in a block of text, the characters < and > are encoded as &lt; and &gt; for HTTP transmission.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

另請參閱

適用於