StringBuilder.Chars[Int32] 屬性

定義

在此實例中,取得或設定字元於指定字元位置。

public:
 property char default[int] { char get(int index); void set(int index, char value); };
public char this[int index] { get; set; }
member this.Chars(int) : char with get, set
Default Public Property Chars(index As Integer) As Char

參數

index
Int32

角色的位置。

屬性值

Unicode 字元位於位置 index

例外狀況

index 在設定角色時,不屬於此實例範圍。

index 在獲得角色時,不屬於此實例範圍。

備註

參數 index 是字元在 StringBuilder中的位置。 字串的第一個字元位於索引 0。 字串的長度是它所包含的字元數。 實例最後可存取的字元 StringBuilder 位於索引 Length - 1。

Chars[Int32] 是該類別的 StringBuilder 預設屬性。 在 C# 中,它是索引器。 這表示可以從 Chars[Int32] 該屬性中取得個別字元,如以下範例所示,該範例計算字串中字母、空白和標點符號的數量。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      int nAlphabeticChars = 0;
      int nWhitespace = 0;
      int nPunctuation = 0;  
      StringBuilder sb = new StringBuilder("This is a simple sentence.");
      
      for (int ctr = 0; ctr < sb.Length; ctr++) {
         char ch = sb[ctr];
         if (Char.IsLetter(ch)) { nAlphabeticChars++;  continue; }
         if (Char.IsWhiteSpace(ch)) { nWhitespace++;  continue; }
         if (Char.IsPunctuation(ch)) nPunctuation++;  
      }    

      Console.WriteLine("The sentence '{0}' has:", sb);
      Console.WriteLine("   Alphabetic characters: {0}", nAlphabeticChars);
      Console.WriteLine("   White-space characters: {0}", nWhitespace);
      Console.WriteLine("   Punctuation characters: {0}", nPunctuation);
   }
}
// The example displays the following output:
//       The sentence 'This is a simple sentence.' has:
//          Alphabetic characters: 21
//          White-space characters: 4
//          Punctuation characters: 1
open System
open System.Text

let mutable nAlphabeticChars = 0
let mutable nWhitespace = 0
let mutable nPunctuation = 0  
let sb = StringBuilder "This is a simple sentence."

for i = 0 to sb.Length - 1 do
    let ch = sb[i]
    if Char.IsLetter ch then
        nAlphabeticChars <- nAlphabeticChars + 1
    elif Char.IsWhiteSpace ch then
        nWhitespace <- nWhitespace + 1
    elif Char.IsPunctuation ch then
        nPunctuation <- nPunctuation + 1

printfn $"The sentence '{sb}' has:"
printfn $"   Alphabetic characters: {nAlphabeticChars}"
printfn $"   White-space characters: {nWhitespace}"
printfn $"   Punctuation characters: {nPunctuation}"

// The example displays the following output:
//       The sentence 'This is a simple sentence.' has:
//          Alphabetic characters: 21
//          White-space characters: 4
//          Punctuation characters: 1
Imports System.Text

Module Example
   Public Sub Main()
      Dim nAlphabeticChars As Integer = 0
      Dim nWhitespace As Integer = 0
      Dim nPunctuation As Integer = 0  
      Dim sb As New StringBuilder("This is a simple sentence.")
      
      For ctr As Integer = 0 To sb.Length - 1
         Dim ch As Char = sb(ctr)
         If Char.IsLetter(ch) Then nAlphabeticChars += 1 : Continue For
         If Char.IsWhiteSpace(ch) Then nWhitespace += 1 : Continue For
         If Char.IsPunctuation(ch) Then nPunctuation += 1
      Next    

      Console.WriteLine("The sentence '{0}' has:", sb)
      Console.WriteLine("   Alphabetic characters: {0}", nAlphabeticChars)
      Console.WriteLine("   White-space characters: {0}", nWhitespace)
      Console.WriteLine("   Punctuation characters: {0}", nPunctuation)
   End Sub
End Module
' The example displays the following output:
'       The sentence 'This is a simple sentence.' has:
'          Alphabetic characters: 21
'          White-space characters: 4
'          Punctuation characters: 1

在下列狀況下,使用以字元為主的索引與 Chars[] 屬性可能極為緩慢:

每訪問一個字元時,因為需要遍歷整個連結串列的區塊以尋找要檢索的正確緩衝區,所以效能會嚴重受影響。

Note

即使是大型的「區塊」 StringBuilder 物件,使用 Chars[] 屬性進行索引式存取一個或少數幾個字符對效能的影響微乎其微;一般而言,它是 O(n) 的作業。 遍歷 StringBuilder 物件中的字元時,就會對效能產生顯著的影響,這是 O(n^2) 操作。

如果在 StringBuilder 物件中使用以字元為主的索引時遇到效能問題,您可以使用下列任一因應措施:

  • 藉由呼叫 StringBuilder 方法將 String 執行個體轉換為 ToString,然後存取字串中的字元。

  • 將現有 StringBuilder 物件的內容複製到預留大小的新 StringBuilder 物件。 因為新的 StringBuilder 物件不是塊狀,所以會改善效能。 例如:

    // sbOriginal is the existing StringBuilder object
    var sbNew = new StringBuilder(sbOriginal.ToString(), sbOriginal.Length);
    
    ' sbOriginal is the existing StringBuilder object
    Dim sbNew = New StringBuilder(sbOriginal.ToString(), sbOriginal.Length)
    
  • 藉由呼叫 StringBuilder 建構函式,將 StringBuilder(Int32) 物件的初始容量設定為大約等於其預期大小上限的值。 請注意,即使 StringBuilder 很少達到最大容量,仍然會配置整個記憶體區塊。

適用於

另請參閱