HttpUtility.HtmlEncode HttpUtility.HtmlEncode HttpUtility.HtmlEncode HttpUtility.HtmlEncode Method

定義

將字串轉換為 HTML 編碼的字串。Converts a string into an HTML-encoded string.

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

多載

HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object)

將物件的字串表示轉換為 HTML 編碼字串,並傳回編碼的字串。Converts an object's string representation into an HTML-encoded string, and returns the encoded string.

HtmlEncode(String) HtmlEncode(String) HtmlEncode(String) HtmlEncode(String)

將字串轉換成 HTML 編碼字串。Converts a string to an HTML-encoded string.

HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter)

將字串轉換為 HTML 編碼的字串,並以 TextWriter 輸出資料流的形式傳回輸出。Converts a string into an HTML-encoded string, and returns the output as a TextWriter stream of output.

HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object) HtmlEncode(Object)

將物件的字串表示轉換為 HTML 編碼字串,並傳回編碼的字串。Converts an object's string representation into an HTML-encoded string, and returns the encoded string.

public:
 static System::String ^ HtmlEncode(System::Object ^ value);
public static string HtmlEncode (object value);
static member HtmlEncode : obj -> string
Public Shared Function HtmlEncode (value As Object) As String

參數

value
Object Object Object Object

物件。An object.

傳回

編碼字串。An encoded string.

備註

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

HtmlEncode(String) HtmlEncode(String) HtmlEncode(String) HtmlEncode(String)

將字串轉換成 HTML 編碼字串。Converts a string to an HTML-encoded string.

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

參數

s
String String String String

要編碼的字串。The string to encode.

傳回

編碼字串。An encoded string.

範例

下列程式碼範例示範HtmlEncode HttpUtility類別的HtmlDecode和方法。The following code example demonstrates the HtmlEncode and HtmlDecode methods of the HttpUtility class. 輸入字串是使用HtmlEncode方法進行編碼。The input string is encoded using the HtmlEncode method. 接著會使用HtmlDecode方法將取得的編碼字串解碼。The 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
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 'Main
End Class 'MyNewClass

備註

如果在 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. 例如, 內嵌在文字區塊中時, 字元<和 > 會編碼為&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.

另請參閱

HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter)

將字串轉換為 HTML 編碼的字串,並以 TextWriter 輸出資料流的形式傳回輸出。Converts a string into an HTML-encoded string, and returns the output as a TextWriter stream of output.

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

參數

s
String String String String

要編碼的字串The string to encode

output
TextWriter TextWriter TextWriter TextWriter

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

備註

如果在 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. 例如, 內嵌在文字區塊中時, 字元<和 > 會編碼為&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.

另請參閱

適用於