共用方式為


StringBuilder.Chars Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: January 2011

Gets or sets the character at the specified character position in this instance.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Property Chars ( _
    index As Integer _
) As Char
public char this[
    int index
] { get; set; }

Parameters

Property Value

Type: System.Char
The Unicode character at position index.

Exceptions

Exception Condition
ArgumentOutOfRangeException

index is outside the bounds of this instance while setting a character.

IndexOutOfRangeException

index is outside the bounds of this instance while getting a character.

Remarks

The index parameter is the position of a character within the StringBuilder. The first character in the string is at index 0. The length of a string is the number of characters it contains. The last accessible character of a StringBuilder instance is at index Length - 1.

Chars is the default property of the StringBuilder class. In C#, it is an indexer. This means that individual characters can be retrieved from the Chars property as shown in the following example, which counts the number of alphabetic, white-space, and punctuation characters in a string.

Imports System.Text

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      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    

      outputBlock.Text += String.Format("The sentence '{0}' has:", 
                                        sb) + vbCrLf
      outputBlock.Text += String.Format("   Alphabetic characters: {0}", 
                                        nAlphabeticChars) + vbCrLf
      outputBlock.Text += String.Format("   Whitespace characters: {0}", 
                                        nWhitespace) + vbCrLf
      outputBlock.Text += String.Format("   Punctuation characters: {0}", 
                                        nPunctuation) + vbCrLf
   End Sub
End Module
' The example displays the following output:
'       The sentence 'This is a simple sentence.' has:
'          Alphabetic characters: 21
'          Whitespace characters: 4
'          Punctuation characters: 1
using System;
using System.Text;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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++;  
      }    

      outputBlock.Text += String.Format("The sentence '{0}' has:\n", sb);
      outputBlock.Text += String.Format("   Alphabetic characters: {0}\n", 
                                        nAlphabeticChars);
      outputBlock.Text += String.Format("   Whitespace characters: {0}\n", 
                                        nWhitespace);
      outputBlock.Text += String.Format("   Punctuation characters: {0}\n", 
                                        nPunctuation);
   }
}
// The example displays the following output:
//       The sentence 'This is a simple sentence.' has:
//          Alphabetic characters: 21
//          Whitespace characters: 4
//          Punctuation characters: 1

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

January 2011

Expanded the Remarks section.

Customer feedback.