StringBuilder クラスとは? わかりやすく解説

StringBuilder クラス

可変型の文字列表します。このクラス継承できません。

名前空間: System.Text
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 StringBuilder
    Implements ISerializable
Dim instance As StringBuilder
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class StringBuilder : ISerializable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class StringBuilder sealed : ISerializable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class StringBuilder implements
 ISerializable
SerializableAttribute 
ComVisibleAttribute(true) 
public final class StringBuilder implements
 ISerializable
解説解説

このクラスは、可変型の文字シーケンスである値を持つ、文字列のようなオブジェクト表します文字追加削除置換または挿入して値を作成した後にその値を変更できるため、値が可変型と呼ばれます比較のため、String クラストピック参照してください

このクラスインスタンス変更するメソッド多くは、同じインスタンスへの参照返しますインスタンスへの参照返されるため、参照に対してメソッドまたはプロパティ呼び出すことができます。これは、連続した操作次々チェイン化する単一ステートメントを書く場合役立ちます

StringBuilder容量は、インスタンス指定した時間格納できる最大文字数で、インスタンスの値の文字列形式長さ上です。容量は、Capacity プロパティまたは EnsureCapacity メソッド使用して増減できます。ただし、Length プロパティの値より小さくすることはできません。

StringBuilderインスタンス初期化されるときに、容量または最大容量指定されなかった場合は、実装固有の既定容量使用されます。

パフォーマンスに関する考慮事項

Concat メソッドと AppendFormat メソッドは、どちらも新しデータ既存String オブジェクトまたは StringBuilder オブジェクト連結します。String オブジェクト連結演算は、常に既存文字列新しデータから新しオブジェクト作成します一方StringBuilder オブジェクトは、新しデータ連結使用するためのバッファ保持しますバッファ十分な容量がある場合は、新しデータバッファ末尾追加されます。容量足りない場合は、より大きなバッファ新しく割り当てられ、元のバッファから新しバッファデータコピーされたうえで、新しデータ新しバッファ追加されます。

String または StringBuilder オブジェクト連結演算パフォーマンスは、メモリ割り当て頻度依存してます。String連結演算では、常にメモリ割り当てられます。一方StringBuilder連結演算では、StringBuilder オブジェクトバッファ新しデータのための十分な容量ない場合にのみ、メモリ割り当てられます。たがって連結する String オブジェクトの数が決まっている場合は、String クラス使用した方が効率的です。この場合個々連結演算は、コンパイラによって 1 つ演算結合されます。これに対しランダムな数の文字列ユーザーから入力として受け取りループ処理で連結する場合など、連結する文字列の数が不定である場合は、StringBuilder オブジェクト適してます。

実装時の注意 この実装既定容量16 で、既定最大容量は Int32.MaxValue です。 StringBuilder は、インスタンスの値が大きくなった場合に、文字格納するために必要なより多くメモリ割り当てることができます。それに応じて容量調整されます。割り当てられメモリの量が実装固有で、必要なメモリ量が最大容量より大きい場合は、ArgumentOutOfRangeException がスローさます。 たとえば、Append メソッド、AppendFormat メソッドEnsureCapacity メソッドInsert メソッド、および Replace メソッドインスタンスの値を拡大できますStringBuilder の値内の個別文字は、Chars プロパティ使用してアクセスできますインデックス位置は 0 から始まります

使用例使用例

次のコード例は、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.Object
  System.Text.StringBuilder
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「StringBuilder クラス」の関連用語

StringBuilder クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



StringBuilder クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS