次の方法で共有


StringBuilder.Append メソッド (String, Int32, Int32)

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

Overloads Public Function Append( _
   ByVal value As String, _   ByVal startIndex As Integer, _   ByVal count As Integer _) As StringBuilder
[C#]
public StringBuilder Append(stringvalue,intstartIndex,intcount);
[C++]
public: StringBuilder* Append(String* value,intstartIndex,intcount);
[JScript]
public function Append(
   value : String,startIndex : int,count : int) : StringBuilder;

パラメータ

  • value
    追加する String
  • startIndex
    value 内の開始位置。
  • count
    追加する文字数。

戻り値

追加操作が発生した後のこのインスタンスへの参照。

例外

例外の種類 条件
ArgumentNullException value が null 参照 (Visual Basic では Nothing) であり、 startIndexcount が 0 以外の値です。
ArgumentOutOfRangeException count が 0 未満です。

または

startIndex が 0 未満です。

または

startIndexcount を加算した値が value の長さより小さい値です。

または

このインスタンスの値を増やすと、 MaxCapacity を超えます。

解説

このメソッドは、 value 内の指定した範囲の文字を現在のインスタンスに追加します。 value が null 参照 (Visual Basic では Nothing) であり、 startIndexcount がいずれも 0 の場合、変更は実行されません。

容量は、必要に応じて調整されます。

使用例

 
' This example demonstrates StringBuilder.Append()
Imports System
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sep As String = ", "
      Dim head As String = "<<<"
      Dim tail As Char() =  {">"c, ">"c, ">"c}
      Dim dash As Char = "-"c
      Dim obj As [Object] = 0
      
      Dim xBool As Boolean = True
      Dim xByte As Byte = 1
      Dim xInt16 As Short = 2
      Dim xInt32 As Integer = 3
      Dim xInt64 As Long = 4
      Dim xDecimal As [Decimal] = 5
      Dim xSingle As Single = 6.6F
      Dim xDouble As Double = 7.7
      
      ' The following types are not CLS-compliant.
      ' Dim xUInt16 As System.UInt16 = 8
      ' Dim xUInt32 As System.UInt32 = 9
      ' Dim xUInt64 As System.UInt64 = 10
      ' Dim xSByte As System.SByte = - 11
      '
      Dim sb As New StringBuilder()
      
      sb = sb.Append(head)              ' <<<
      sb = sb.Append(head, 2, 1)        ' <<<<
      sb = sb.Append(dash)              ' <<<<-
      sb = sb.Append(dash).Append(dash) ' <<<<---
      sb = sb.Append(xBool).Append(sep)
      sb = sb.Append(obj).Append(sep).Append(xByte).Append(sep)
      sb = sb.Append(xInt16)
      sb = sb.Append(sep)
      sb = sb.Append(xInt32)
      sb = sb.Append(sep)
      sb = sb.Append(xInt64)
      sb = sb.Append(sep)
      sb = sb.Append(xDecimal).Append(sep)
      sb = sb.Append(xSingle).Append(sep).Append(xDouble)
      
      ' The following Append methods are not CLS-compliant.
      ' sb.Append(xUInt16)
      ' sb.Append(xUInt32)
      ' sb.Append(xUInt64)
      ' sb.Append(xSByte)
      '
      sb = sb.Append(dash, 3)           ' ---
      sb = sb.Append(tail)              ' --->>>
      sb = sb.Append(tail, 2, 1)        ' --->>>>
      Dim str As [String] = sb.ToString()
      Console.WriteLine("The appended string is:")
      Console.WriteLine(str)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'The appended string is:
'<<<<---True, 0, 1, 2, 3, 4, 5, 6.6, 7.7--->>>>
'

[C#] 
// This example demonstrates StringBuilder.Append()
using System;
using System.Text;

class Sample 
{
    public static void Main() 
    {
    string      sep       = ", ";
    string      head      = "<<<";
    char[]      tail      = {'>', '>', '>'};
    char        dash      = '-';
    Object     obj       = 0;

    bool        xBool     = true;
    byte        xByte     = 1;
    short       xInt16    = 2;
    int         xInt32    = 3;
    long        xInt64    = 4;
    Decimal     xDecimal  = 5;
    float       xSingle   = 6.6F;
    double      xDouble   = 7.7;

// The following types are not CLS-compliant.
    ushort      xUInt16   = 8;
    uint        xUInt32   = 9;
    ulong       xUInt64   = 10;
    sbyte       xSByte    = -11;
//
    StringBuilder sb = new StringBuilder();

    sb = sb.Append(head);                // <<<
    sb = sb.Append(head, 2, 1);          // <<<<
    sb = sb.Append(dash);                // <<<<-
    sb = sb.Append(dash).Append(dash);   // <<<<---

    sb = sb.Append(xBool).Append(sep);
    sb = sb.Append(obj).Append(sep).Append(xByte).Append(sep);
    sb = sb.Append(xInt16);
    sb = sb.Append(sep);
    sb = sb.Append(xInt32);
    sb = sb.Append(sep);
    sb = sb.Append(xInt64);
    sb = sb.Append(sep);
    sb = sb.Append(xDecimal).Append(sep);
    sb = sb.Append(xSingle).Append(sep).Append(xDouble).Append(sep);

// The following Append methods are not CLS-compliant.
    sb = sb.Append(xUInt16).Append(sep);
    sb = sb.Append(xUInt32).Append(sep).Append(xUInt64).Append(sep);
    sb = sb.Append(xSByte);
//
    sb = sb.Append(dash, 3);             // ---
    sb = sb.Append(tail);                // --->>>
    sb = sb.Append(tail, 2, 1);          // --->>>>
   
    String str = sb.ToString();
    Console.WriteLine("The appended string is:");
    Console.WriteLine(str);
    }
}
/*
This example produces the following results:
The appended string is:
<<<<---True, 0, 1, 2, 3, 4, 5, 6.6, 7.7, 8, 9, 10, -11--->>>>
*/

[C++] 
// This example demonstrates StringBuilder.Append()
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main() 
{
    String*     sep       = S", ";
    String*     head      = S"<<<";
    Char tail[]           = {'>', '>', '>'};
    Char        dash      = '-';
    Object*     obj       = __box(0);

    bool        xBool     = true;
    Byte        xByte     = 1;
    short       xInt16    = 2;
    int         xInt32    = 3;
    long        xInt64    = 4;
    Decimal     xDecimal  = 5;
    float       xSingle   = 6.6F;
    double      xDouble   = 7.7;

// The following types are not CLS-compliant.
    UInt16      xUInt16   = 8;
    UInt32      xUInt32   = 9;
    UInt64      xUInt64   = 10;
    SByte       xSByte    = -11;
//
    StringBuilder* sb = new StringBuilder();

    sb = sb->Append(head);                // <<<
    sb = sb->Append(head, 2, 1);          // <<<<
    sb = sb->Append(dash);                // <<<<-
    sb = sb->Append(dash)->Append(dash);  // <<<<---

    sb = sb->Append(xBool)->Append(sep);
    sb = sb->Append(obj)->Append(sep)->Append(xByte)->Append(sep);
    sb = sb->Append(xInt16);
    sb = sb->Append(sep);
    sb = sb->Append(xInt32);
    sb = sb->Append(sep);
    sb = sb->Append(xInt64);
    sb = sb->Append(sep);
    sb = sb->Append(xDecimal)->Append(sep);
    sb = sb->Append(xSingle)->Append(sep)->Append(xDouble)->Append(sep);

// The following Append methods are not CLS-compliant.
    sb = sb->Append(xUInt16)->Append(sep);
    sb = sb->Append(xUInt32)->Append(sep)->Append(xUInt64)->Append(sep);
    sb = sb->Append(xSByte);
//
    sb = sb->Append(dash, 3);             // ---
    sb = sb->Append(tail);                // --->>>
    sb = sb->Append(tail, 2, 1);          // --->>>>
   
    String* str = sb->ToString();
    Console::WriteLine(S"The appended string is:");
    Console::WriteLine(str);
}
/*
This example produces the following results:
The appended string is:
<<<<---True, 0, 1, 2, 3, 4, 5, 6.6, 7.7, 8, 9, 10, -11--->>>>
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

StringBuilder クラス | StringBuilder メンバ | System.Text 名前空間 | StringBuilder.Append オーバーロードの一覧 | String