StringBuilder クラス

定義

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

public ref class StringBuilder sealed
public ref class StringBuilder sealed : System::Runtime::Serialization::ISerializable
public sealed class StringBuilder
public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
[System.Serializable]
public sealed class StringBuilder
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StringBuilder : System.Runtime.Serialization.ISerializable
type StringBuilder = class
type StringBuilder = class
    interface ISerializable
[<System.Serializable>]
type StringBuilder = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringBuilder = class
    interface ISerializable
Public NotInheritable Class StringBuilder
Public NotInheritable Class StringBuilder
Implements ISerializable
継承
StringBuilder
属性
実装

次の例は、 クラスで定義されているメソッドの多くを呼び出す方法を StringBuilder 示しています。

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
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
open System.Text

// Create a StringBuilder that expects to hold 50 characters.
// Initialize the StringBuilder with "ABC".
let sb = StringBuilder("ABC", 50)

// Append three characters (D, E, and F) to the end of the StringBuilder.
sb.Append [| 'D'; 'E'; 'F' |] |> ignore

// Append a format string to the end of the StringBuilder.
sb.AppendFormat("GHI{0}{1}", 'J', 'k') |> ignore

// Display the number of characters in the StringBuilder and its string.
printfn $"{sb.Length} chars: {sb}"

// Insert a string at the beginning of the StringBuilder.
sb.Insert(0, "Alphabet: ") |> ignore

// Replace all lowercase k's with uppercase K's.
sb.Replace('k', 'K') |> ignore

// Display the number of characters in the StringBuilder and its string.
printfn $"{sb.Length} chars: {sb}"

// This code produces the following output.
//
// 11 chars: ABCDEFGHIJk
// 21 chars: Alphabet: ABCDEFGHIJK
Imports System.Text

Public Module App 
    Public Sub Main() 
        ' Create a StringBuilder that expects to hold 50 characters.
        ' Initialize the StringBuilder with "ABC".
        Dim sb As New StringBuilder("ABC", 50)

        ' Append three characters (D, E, and F) to the end of the StringBuilder.
        sb.Append(New Char() {"D"c, "E"c, "F"c})

        ' Append a format string to the end of the StringBuilder.
        sb.AppendFormat("GHI{0}{1}", "J"c, "k"c)

        ' 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())
    End Sub
End Module

' This code produces the following output.
'
' 11 chars: ABCDEFGHIJk
' 21 chars: Alphabet: ABCDEFGHIJK

注釈

この API の詳細については、「 StringBuilder の補足 API 解説」を参照してください。

注意 (呼び出し元)

.NET Core および .NET Framework 4.0 以降のバージョンでは、コンストラクターを呼び出StringBuilder(Int32, Int32)してオブジェクトをインスタンス化StringBuilderすると、インスタンスの長さと容量の両方がそのMaxCapacityプロパティのStringBuilder値を超えて増加する可能性があります。 これは、特に、 メソッドと AppendFormat(String, Object) メソッドをAppend(String)呼び出して小さな文字列を追加する場合に発生する可能性があります。

コンストラクター

StringBuilder()

StringBuilder クラスの新しいインスタンスを初期化します。

StringBuilder(Int32)

指定した容量を使用して、StringBuilder クラスの新しいインスタンスを初期化します。

StringBuilder(Int32, Int32)

指定した容量で始まり、指定した最大容量まで大きくなる StringBuilder クラスの新しいインスタンスを初期化します。

StringBuilder(String)

指定した文字列を使用して、StringBuilder クラスの新しいインスタンスを初期化します。

StringBuilder(String, Int32)

指定した文字列および容量を使用して、StringBuilder クラスの新しいインスタンスを初期化します。

StringBuilder(String, Int32, Int32, Int32)

指定した部分文字列および容量から StringBuilder クラスの新しいインスタンスを初期化します。

プロパティ

Capacity

現在のインスタンスによって割り当てられたメモリに格納できる最大文字数を取得または設定します。

Chars[Int32]

このインスタンス内の指定した文字位置の文字を取得または設定します。

Length

現在の StringBuilder オブジェクトの長さを取得または設定します。

MaxCapacity

このインスタンスの最大容量を取得します。

メソッド

Append(Boolean)

指定した Boolean 値の文字列形式をこのインスタンスに追加します。

Append(Byte)

指定した 8 ビット符号なし整数の文字列形式をこのインスタンスに追加します。

Append(Char)

指定した Char オブジェクトの文字列形式をこのインスタンスに追加します。

Append(Char*, Int32)

指定したアドレスで始まる Unicode 文字の配列をこのインスタンスに追加します。

Append(Char, Int32)

Unicode 文字の文字列形式の、指定した数のコピーをこのインスタンスに追加します。

Append(Char[])

指定した配列内の Unicode 文字の文字列形式をこのインスタンスに追加します。

Append(Char[], Int32, Int32)

Unicode 文字の指定した部分配列の文字列形式をこのインスタンスに追加します。

Append(Decimal)

指定した 10 進数の文字列形式をこのインスタンスに追加します。

Append(Double)

指定した倍精度浮動小数点数の文字列形式をこのインスタンスに追加します。

Append(IFormatProvider, StringBuilder+AppendInterpolatedStringHandler)

指定した書式を使用して、指定した補間文字列をこのインスタンスに追加します。

Append(Int16)

指定した 16 ビット符号付き整数の文字列形式をこのインスタンスに追加します。

Append(Int32)

指定した 32 ビット符号付き整数の文字列形式をこのインスタンスに追加します。

Append(Int64)

指定した 64 ビット符号付き整数の文字列形式をこのインスタンスに追加します。

Append(Object)

指定したオブジェクトの文字列形式をこのインスタンスに追加します。

Append(ReadOnlyMemory<Char>)

指定された読み取り専用文字メモリ領域の文字列形式をこのインスタンスに追加します。

Append(ReadOnlySpan<Char>)

指定された読み取り専用文字スパンの文字列形式をこのインスタンスに追加します。

Append(SByte)

指定した 8 ビット符号付き整数の文字列形式をこのインスタンスに追加します。

Append(Single)

指定した単精度浮動小数点数の文字列形式をこのインスタンスに追加します。

Append(String)

指定した文字列のコピーをこのインスタンスに追加します。

Append(String, Int32, Int32)

指定した部分文字列のコピーをこのインスタンスに追加します。

Append(StringBuilder)

指定された文字列ビルダーの文字列形式をこのインスタンスに追加します。

Append(StringBuilder, Int32, Int32)

指定された文字列ビルダー内の部分文字列のコピーをこのインスタンスに追加します。

Append(StringBuilder+AppendInterpolatedStringHandler)

指定した補間文字列をこのインスタンスに追加します。

Append(UInt16)

指定した 16 ビット符号なし整数の文字列形式をこのインスタンスに追加します。

Append(UInt32)

指定された 32 ビット符号なし整数の文字列表記をこのインスタンスに追加します。

Append(UInt64)

指定した 64 ビット符号なし整数の文字列形式をこのインスタンスに追加します。

AppendFormat(IFormatProvider, CompositeFormat, Object[])

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式指定項目は、指定された書式プロバイダーを使用して、任意の引数の文字列表現に置き換えられます。

AppendFormat(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式指定項目は、指定された書式プロバイダーを使用して、任意の引数の文字列表現に置き換えられます。

AppendFormat(IFormatProvider, String, Object)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式指定項目は、指定された書式プロバイダーを使用して単一の引数の文字列形式に置換されます。

AppendFormat(IFormatProvider, String, Object, Object)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式項目は、指定された書式プロバイダーを使用して 2 つの引数のいずれかの文字列形式に置換されます。

AppendFormat(IFormatProvider, String, Object, Object, Object)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式項目は、指定された書式プロバイダーを使用して 3 つの引数のいずれかの文字列形式に置換されます。各書式項目は、指定された書式プロバイダーを使用して 3 つの引数のいずれかの文字列形式に置換されます。

AppendFormat(IFormatProvider, String, Object[])

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式項目は、指定された書式プロバイダーを使用した、パラメーター配列内の対応する引数の文字列形式に置換されます。

AppendFormat(String, Object)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式項目は、単一の引数の文字列表記に置換されます。

AppendFormat(String, Object, Object)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式項目は、2 つの引数のどちらかの文字列形式に置換されます。

AppendFormat(String, Object, Object, Object)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式項目は、3 つの引数のいずれかの文字列形式に置換されます。

AppendFormat(String, Object[])

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式項目は、パラメーター配列内の対応する引数の文字列形式に置換されます。

AppendFormat<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式指定項目は、指定された書式プロバイダーを使用して、任意の引数の文字列表現に置き換えられます。

AppendFormat<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式指定項目は、指定された書式プロバイダーを使用して、任意の引数の文字列表現に置き換えられます。

AppendFormat<TArg0>(IFormatProvider, CompositeFormat, TArg0)

0 個以上の書式項目を含んでいる複合書式指定文字列を処理することで返される文字列を、このインスタンスに追加します。 各書式指定項目は、指定された書式プロバイダーを使用して、任意の引数の文字列表現に置き換えられます。

AppendJoin(Char, Object[])

指定したオブジェクト配列内の要素の文字列表現を連結します。各メンバー間には、指定した区切り文字が使用され、その結果は文字列ビルダーの現在のインスタンスに追加されます。

AppendJoin(Char, String[])

指定した配列の文字列を連結します。各文字列間には、指定した区切り文字が使用され、その結果は文字列ビルダーの現在のインスタンスに追加されます。

AppendJoin(String, Object[])

指定したオブジェクト配列内の要素の文字列表現を連結します。各メンバー間には、指定した区切り記号が使用され、その結果は文字列ビルダーの現在のインスタンスに追加されます。

AppendJoin(String, String[])

指定した配列の文字列を連結します。各文字列間には、指定した区切り記号が使用され、その結果は文字列ビルダーの現在のインスタンスに追加されます。

AppendJoin<T>(Char, IEnumerable<T>)

コレクションのメンバーを連結および追加します。各メンバー間には、指定した区切り文字が使用されます。

AppendJoin<T>(String, IEnumerable<T>)

コレクションのメンバーを連結および追加します。各メンバー間には、指定した区切り記号が使用されます。

AppendLine()

既定の行終端記号を現在の StringBuilder オブジェクトの末尾に追加します。

AppendLine(IFormatProvider, StringBuilder+AppendInterpolatedStringHandler)

現在の StringBuilder オブジェクトの末尾に、指定した形式と既定の行終端記号を使用して、指定した補間文字列を追加します。

AppendLine(String)

指定した文字列のコピーと既定の行終端記号を、現在の StringBuilder オブジェクトの末尾に追加します。

AppendLine(StringBuilder+AppendInterpolatedStringHandler)

現在の StringBuilder オブジェクトの末尾に、指定した補間文字列の後に既定の行終端記号を追加します。

Clear()

現在の StringBuilder インスタンスからすべての文字を削除します。

CopyTo(Int32, Char[], Int32, Int32)

このインスタンスの指定したセグメントにある文字を、目的の Char 配列の指定したセグメントにコピーします。

CopyTo(Int32, Span<Char>, Int32)

文字をこのインスタンスの指定したセグメントから目的の Char のスパンにコピーします。

EnsureCapacity(Int32)

このインスタンスの StringBuilder の容量が、指定した値以上になるようにします。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Equals(ReadOnlySpan<Char>)

このインスタンスの文字が、指定された読み取り専用の文字範囲内の文字と同じであるかどうかを示す値を返します。

Equals(StringBuilder)

このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。

GetChunks()

この StringBuilder インスタンスから作成された ReadOnlyMemory<Char> で表される文字のチャンクを反復処理する目的で利用できるオブジェクトを返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
Insert(Int32, Boolean)

Boolean 値の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Byte)

指定した 8 ビット符号なし整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Char)

指定した Unicode 文字の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Char[])

指定した Unicode 文字の配列の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Char[], Int32, Int32)

Unicode 文字の指定した部分配列の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Decimal)

10 進数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Double)

倍精度浮動小数点数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Int16)

指定した 16 ビット符号付き整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Int32)

指定した 32 ビット符号付き整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Int64)

64 ビット符号付き整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Object)

オブジェクトの文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, ReadOnlySpan<Char>)

文字のシーケンスをこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, SByte)

指定した 8 ビット符号付き整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, Single)

単精度浮動小数点数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, String)

文字列をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, String, Int32)

指定した文字列の 1 つ以上のコピーをこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, UInt16)

16 ビット符号なし整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, UInt32)

32 ビット符号なし整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

Insert(Int32, UInt64)

64 ビット符号なし整数の文字列形式をこのインスタンスの指定した文字位置に挿入します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(Int32, Int32)

このインスタンスから、指定した範囲の文字を削除します。

Replace(Char, Char)

このインスタンスに出現する指定文字をすべて、別に指定した文字に置換します。

Replace(Char, Char, Int32, Int32)

このインスタンスの部分文字列に出現するすべての指定した文字を、別の指定した文字に置換します。

Replace(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

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

Replace(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Int32, Int32)

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

Replace(String, String)

このインスタンスに出現するすべての指定した文字列を、別の指定した文字列に置換します。

Replace(String, String, Int32, Int32)

このインスタンスの部分文字列に出現するすべての指定した文字列を、別の指定した文字列に置換します。

ToString()

このインスタンスの値を String に変換します。

ToString(Int32, Int32)

このインスタンスの部分文字列の値を String に変換します。

明示的なインターフェイスの実装

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

SerializationInfo オブジェクトに、現在の StringBuilder オブジェクトの逆シリアル化に必要なデータを入力します。

適用対象

こちらもご覧ください