更新 : 2007 年 11 月
可変型の文字列を表します。このクラスは継承できません。
名前空間 :
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
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 から始まります。
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォーム メモ :
文字列を StringBuilder オブジェクトに継続的に追加すると、仮想アドレス空間の断片化が発生する場合があります。Windows CE では、各プロセスの仮想アドレス空間の制限によって、約 3 MB の文字列を追加するとこの問題が発生する可能性があります。この問題を回避するには、MaxCapacity プロパティを設定します。または、MemoryStream を開き、StringBuilder クラスを使わずに文字列をメモリ ストリームに書き込みます。
次のコード例は、StringBuilder クラスで定義された多くのメソッドを呼び出す方法を示しています。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Samples</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<script type="text/javascript">
function buildAString(title){
var headTagStart = "<head>";
var headTagEnd = "</head>";
var titleTagStart = "<title>";
var titleTagEnd = "</title>";
var sb = new Sys.StringBuilder(this._headTagStart);
sb.append(titleTagEnd);
sb.append(title);
sb.append(titleTagEnd);
sb.append(headTagEnd);
// Displays: "The result: <head><title>A Title</title></head>"
alert("The result" + sb.toString());
}
var title = "A Title";
buildAString(title);
</script>
</form>
</body>
</html>
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
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
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 3.5、2.0、1.0
XNA Framework
サポート対象 : 2.0、1.0
参照