StringBuilder クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class StringBuilder Implements ISerializable
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class StringBuilder : ISerializable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class StringBuilder sealed : ISerializable
このクラスは、可変型の文字のシーケンスである値を持つ、文字列のようなオブジェクトを表します。文字を追加、削除、置換または挿入して値を作成した後にその値を変更できるため、値が可変型と呼ばれます。比較のため、String クラスのトピックを参照してください。
このクラスのインスタンスを変更するメソッドの多くは、同じインスタンスへの参照を返します。インスタンスへの参照が返されるため、参照に対してメソッドまたはプロパティを呼び出すことができます。これは、連続した操作を次々にチェイン化する単一のステートメントを書く場合に役立ちます。
StringBuilder の容量は、インスタンスが指定した時間に格納できる最大文字数で、インスタンスの値の文字列形式の長さ以上です。容量は、Capacity プロパティまたは EnsureCapacity メソッドを使用して増減できます。ただし、Length プロパティの値より小さくすることはできません。
StringBuilder のインスタンスが初期化されるときに、容量または最大容量が指定されなかった場合は、実装に固有の既定の容量が使用されます。
パフォーマンスに関する考慮事項Concat メソッドと AppendFormat メソッドは、どちらも新しいデータを既存の String オブジェクトまたは StringBuilder オブジェクトに連結します。String オブジェクトの連結演算は、常に既存の文字列と新しいデータから新しいオブジェクトを作成します。一方、StringBuilder オブジェクトは、新しいデータの連結に使用するためのバッファを保持します。バッファに十分な容量がある場合は、新しいデータがバッファの末尾に追加されます。容量が足りない場合は、より大きなバッファが新しく割り当てられ、元のバッファから新しいバッファにデータがコピーされたうえで、新しいデータが新しいバッファに追加されます。
String または StringBuilder オブジェクトの連結演算のパフォーマンスは、メモリの割り当て頻度に依存しています。String の連結演算では、常にメモリが割り当てられます。一方、StringBuilder の連結演算では、StringBuilder オブジェクトのバッファに新しいデータのための十分な容量がない場合にのみ、メモリが割り当てられます。したがって、連結する String オブジェクトの数が決まっている場合は、String クラスを使用した方が効率的です。この場合、個々の連結演算は、コンパイラによって 1 つの演算に結合されます。これに対し、ランダムな数の文字列をユーザーから入力として受け取り、ループ処理で連結する場合など、連結する文字列の数が不定である場合は、StringBuilder オブジェクトが適しています。
次のコード例は、StringBuilder クラスで定義された多くのメソッドを呼び出す方法を示しています。
using System; using System.Text; public sealed class App { static void Main() { // Create a StringBuilder that expects to hold 50 characters. // Initialize the StringBuilder with "ABC". StringBuilder sb = new StringBuilder("ABC", 50); // Append three characters (D, E, and F) to the end of the StringBuilder. sb.Append(new char[] { 'D', 'E', 'F' }); // Append a format string to the end of the StringBuilder. sb.AppendFormat("GHI{0}{1}", 'J', 'k'); // Display the number of characters in the StringBuilder and its string. Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString()); // Insert a string at the beginning of the StringBuilder. sb.Insert(0, "Alphabet: "); // Replace all lowercase k's with uppercase K's. sb.Replace('k', 'K'); // Display the number of characters in the StringBuilder and its string. Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString()); } } // This code produces the following output. // // 11 chars: ABCDEFGHIJk // 21 chars: Alphabet: ABCDEFGHIJK
using namespace System; using namespace System::Text; int main() { // Create a StringBuilder that expects to hold 50 characters. // Initialize the StringBuilder with "ABC". StringBuilder^ sb = gcnew StringBuilder("ABC", 50); // Append three characters (D, E, and F) to the end of the // StringBuilder. sb->Append(gcnew array<Char>{'D', 'E', 'F'}); // Append a format string to the end of the StringBuilder. sb->AppendFormat("GHI{0}{1}", (Char)'J', (Char)'k'); // Display the number of characters in the StringBuilder // and its string. Console::WriteLine("{0} chars: {1}", sb->Length, sb->ToString()); // Insert a string at the beginning of the StringBuilder. sb->Insert(0, "Alphabet: "); // Replace all lowercase k's with uppercase K's. sb->Replace('k', 'K'); // Display the number of characters in the StringBuilder // and its string. Console::WriteLine("{0} chars: {1}", sb->Length, sb->ToString()); } // This code produces the following output. // // 11 chars: ABCDEFGHIJk // 21 chars: Alphabet: ABCDEFGHIJK
System.Text.StringBuilder
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- StringBuilder クラスのページへのリンク