UTF8Encoding.GetString(Byte[], Int32, Int32) 方法

定義

將位元組陣列中的某一段位元組範圍解碼成字串。

public:
 override System::String ^ GetString(cli::array <System::Byte> ^ bytes, int index, int count);
public override string GetString (byte[] bytes, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override string GetString (byte[] bytes, int index, int count);
override this.GetString : byte[] * int * int -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.GetString : byte[] * int * int -> string
Public Overrides Function GetString (bytes As Byte(), index As Integer, count As Integer) As String

參數

bytes
Byte[]

包含要解碼之位元組序列的位元組陣列。

index
Int32

要解碼的第一個位元組索引。

count
Int32

要解碼的位元組數。

傳回

String,包含將指定之位元組序列解碼的結果。

屬性

例外狀況

bytesnull

indexcount 小於零。

-或-

indexcount 不代表 bytes 中有效的範圍。

已啟用錯誤偵測,而且 bytes 包含無效的位元組序列。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼)

-和-

DecoderFallback 設定為 DecoderExceptionFallback

範例

下列範例會呼叫 方法來初始化陣列, GetByteCount 以判斷編碼字串所需的位元組數目,然後將位元組順序標記的大小新增 (BOM) 。 然後,此範例會 GetPreamble 呼叫 方法,將 BOM 儲存至陣列,再呼叫 GetBytes 方法,以將編碼的位元組儲存至陣列。 然後,此範例會 GetString 呼叫 方法來解碼字串。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      UTF8Encoding utf8 = new UTF8Encoding(true, true);

      String s = "It was the best of times, it was the worst of times...";

      // We need to dimension the array, since we'll populate it with 2 method calls.
      Byte[] bytes = new Byte[utf8.GetByteCount(s) + utf8.GetPreamble().Length];
      // Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length);
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length);

      // Decode the byte array.
      String s2 = utf8.GetString(bytes, 0, bytes.Length);
      Console.WriteLine(s2);
   }
}
// The example displays the following output:
//        ?It was the best of times, it was the worst of times...
Imports System.Text

Module Example
   Public Sub Main()
      Dim utf8 As New UTF8Encoding(True, True)

      Dim s As String = "It was the best of times, it was the worst of times..."

      ' We need to dimension the array, since we'll populate it with 2 method calls.
      Dim bytes(utf8.GetByteCount(s) + utf8.GetPreamble().Length - 1) As Byte
      ' Encode the string.
      Array.Copy(utf8.GetPreamble(), bytes, utf8.GetPreamble().Length)
      utf8.GetBytes(s, 0, s.Length, bytes, utf8.GetPreamble().Length)

      ' Decode the byte array.
      Dim s2 As String = utf8.GetString(bytes, 0, bytes.Length)
      Console.WriteLine(s2)
   End Sub
End Module
' The example displays the following output:
'       ?It was the best of times, it was the worst of times...

請注意,在此情況下,解碼字串與原始字串不同,因為它以 16 位位元組順序標記 U+FFFD 開頭。 這表示這兩個字串會比較為不相等,而且如果字串是輸出,則 BOM 會顯示為取代字元 「?」。 若要移除字串開頭的 BOM,您可以呼叫 String.TrimStart 方法。

備註

發生錯誤偵測時,不正確序列會導致這個方法擲回 ArgumentException 例外狀況。 若未偵測錯誤,則會忽略不正確序列,而且不會擲回例外狀況。

如果要解碼的位元組範圍包含位元組順序標記 (BOM) ,且位元組陣列是由非 BOM 感知類型的方法傳回,則這個方法所傳回的字元陣列中會包含字元 U+FFFE。 您可以呼叫 String.TrimStart 方法來移除它。

要轉換的資料,例如從資料流程讀取的資料,可能只能在循序區塊中使用。 在此情況下,或者,如果資料量太大,因此需要分成較小的區塊,請分別使用 DecoderEncoder 方法或 方法所提供的 GetDecoderGetEncoder

適用於

另請參閱