StringBuilder.Chars[Int32] StringBuilder.Chars[Int32] StringBuilder.Chars[Int32] StringBuilder.Chars[Int32] Property

定义

获取或设置此实例中指定字符位置处的字符。Gets or sets the character at the specified character position in this instance.

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 Int32 Int32 Int32

字符的位置。The position of the character.

属性值

index 位置处的 Unicode 字符。The Unicode character at position index.

异常

设置字符时,index 在此实例的范围之外。index is outside the bounds of this instance while setting a character.

获取字符时,index 在此实例的范围之外。index is outside the bounds of this instance while getting a character.

注解

参数是中字符的位置。 StringBuilder indexThe index parameter is the position of a character within the StringBuilder. 字符串中的第一个字符位于索引0处。The first character in the string is at index 0. 字符串的长度是它包含的字符数。The length of a string is the number of characters it contains. StringBuilder实例的最后一个可访问字符位于索引Length -1 处。The last accessible character of a StringBuilder instance is at index Length - 1.

Chars[Int32]StringBuilder类的默认属性。Chars[Int32] is the default property of the StringBuilder class. 在C#中,它是一个索引器。In C#, it is an indexer. 这意味着,可以从Chars[Int32]属性中检索各个字符,如下面的示例所示,它对字符串中的字母、空格和标点符号的数目进行计数。This means that individual characters can be retrieved from the Chars[Int32] property as shown in the following example, which counts the number of alphabetic, white-space, and punctuation characters in a string.

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
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

基于性能和字符的索引Performance and character-based indexing

在以下情况下,通过 Chars[Int32] 属性使用基于字符的索引可能会非常缓慢:Using character-based indexing with the Chars[Int32] property can be extremely slow under the following conditions:

因为每次字符访问都会遍历区块的整个链接列表以查找要索引到的正确缓冲区,所以性能受到严重影响。Performance is severely impacted because each character access walks the entire linked list of chunks to find the correct buffer to index into.

备注

即使对于大型“笨重的”StringBuilder 对象,使用 Chars[Int32] 属性对一个或少量字符进行基于索引的访问几乎不影响性能;一般情况下,它是 0(n) 操作 。Even for a large "chunky" StringBuilder object, using the Chars[Int32] property for index-based access to one or a small number of characters has a negligible performance impact; typically, it is an 0(n) operation. 当迭代 StringBuilder 对象中的字符时,会对性能造成显著的影响,这是 O(n^2) 操作 。The significant performance impact occurs when iterating the characters in the StringBuilder object, which is an O(n^2) operation.

如果通过 StringBuilder 对象在使用基于字符的索引时遇到性能问题,可以使用以下任一解决方法:If you encounter performance issues when using character-based indexing with StringBuilder objects, you can use any of the following workarounds:

  • 通过调用 ToString 方法,将 StringBuilder 实例转换为 String,然后访问字符串中的字符。Convert the StringBuilder instance to a String by calling the ToString method, then access the characters in the string.

  • 将现有的 StringBuilder 对象的内容复制到新的预调整大小的 StringBuilder 对象。Copy the contents of the existing StringBuilder object to a new pre-sized StringBuilder object. 由于新的 StringBuilder 对象并不笨重,因此可以提升性能。Performance improves because the new StringBuilder object is not chunky. 例如:For example:

    // 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 对象的初始容量设置为约等于其最大预期大小的值。Set the initial capacity of the StringBuilder object to a value that is approximately equal to its maximum expected size by calling the StringBuilder(Int32) constructor. 请注意,即使 StringBuilder 很少达到其最大容量,它仍会分配整个内存块。Note that this allocates the entire block of memory even if the StringBuilder rarely reaches its maximum capacity.

适用于

另请参阅