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

字元的位置。

屬性值

位置 index 上的 Unicode 字元。

例外狀況

設定字元時,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[] 屬性可能極為緩慢:

由於每個字元的存取會行經整個連結的區塊清單來尋找要編入索引的正確緩衝區,因此會嚴重影響效能。

注意

即使是大型「區塊」 StringBuilder 物件,使用 Chars[] 屬性來存取一或少數位符的索引型,對效能有可忽略的影響;一般而言,它是 O (n 個) 作業。 逐一查看 StringBuilder 物件中的字元時,就會發生顯著的效能影響,這是 O(n^2) 作業。

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

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

  • 將現有 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(Int32) 建構函式,將 StringBuilder 物件的初始容量設定為大約等於其預期大小上限的值。 請注意,即使 StringBuilder 很少達到其最大容量,這也會配置整個記憶體區塊。

適用於

另請參閱