StringBuilder.Replace メソッド (String, String)

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

Overloads Public Function Replace( _
   ByVal oldValue As String, _   ByVal newValue As String _) As StringBuilder
[C#]
public StringBuilder Replace(stringoldValue,stringnewValue);
[C++]
public: StringBuilder* Replace(String* oldValue,String* newValue);
[JScript]
public function Replace(
   oldValue : String,newValue : String) : StringBuilder;

パラメータ

  • oldValue
    置換する文字列。
  • newValue
    oldValue を置換する文字列。

戻り値

oldValue のすべてのインスタンスが newValue に置換されたこのインスタンスへの参照。

例外

例外の種類 条件
ArgumentNullException oldValue が null 参照 (Visual Basic では Nothing) です。
ArgumentException oldvalue の長さが 0 です。
ArgumentOutOfRangeException このインスタンスの値を増やすと、 MaxCapacity を超えます。

解説

置換する文字列は、序数に基づいてチェックされます。つまり、置換はカルチャを認識しません。 newValue が null 参照 (Visual Basic では Nothing) の場合は、見つかったすべての oldValue が削除されます。このメソッドでは、大文字と小文字が区別されます。

使用例

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

Class Sample
   Public Shared Sub Main()
      '                    0----+----1----+----2----+----3----+----4---
      '                    01234567890123456789012345678901234567890123
      Dim str As String = "The quick br!wn d#g jumps #ver the lazy cat."
      Dim sb As New StringBuilder(str)
      
      Console.WriteLine()
      Console.WriteLine("StringBuilder.Replace method")
      Console.WriteLine()
      
      Console.WriteLine("Original value:")
      Show(sb)
      
      sb.Replace("#"c, "!"c, 15, 29)   ' Some '#' -> '!'
      Show(sb)
      sb.Replace("!"c, "o"c)           ' All '!' -> 'o'
      Show(sb)
      sb.Replace("cat", "dog")         ' All "cat" -> "dog"
      Show(sb)
      sb.Replace("dog", "fox", 15, 20) ' Some "dog" -> "fox"
      Console.WriteLine("Final value:")
      Show(sb)
   End Sub 'Main
   
   Public Shared Sub Show(sbs As StringBuilder)
      Dim rule1 As String = "0----+----1----+----2----+----3----+----4---"
      Dim rule2 As String = "01234567890123456789012345678901234567890123"
      
      Console.WriteLine(rule1)
      Console.WriteLine(rule2)
      Console.WriteLine("{0}", sbs.ToString())
      Console.WriteLine()
   End Sub 'Show
End Class 'Sample
'
'This example produces the following results:
'
'StringBuilder.Replace method
'
'Original value:
'0----+----1----+----2----+----3----+----4---
'01234567890123456789012345678901234567890123
'The quick br!wn d#g jumps #ver the lazy cat.
'
'0----+----1----+----2----+----3----+----4---
'01234567890123456789012345678901234567890123
'The quick br!wn d!g jumps !ver the lazy cat.
'
'0----+----1----+----2----+----3----+----4---
'01234567890123456789012345678901234567890123
'The quick brown dog jumps over the lazy cat.
'
'0----+----1----+----2----+----3----+----4---
'01234567890123456789012345678901234567890123
'The quick brown dog jumps over the lazy dog.
'
'Final value:
'0----+----1----+----2----+----3----+----4---
'01234567890123456789012345678901234567890123
'The quick brown fox jumps over the lazy dog.
'

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

class Sample 
{
    public static void Main() 
    {
//                0----+----1----+----2----+----3----+----4---
//                01234567890123456789012345678901234567890123
    string str = "The quick br!wn d#g jumps #ver the lazy cat.";
    StringBuilder sb = new StringBuilder(str);

    Console.WriteLine();
    Console.WriteLine("StringBuilder.Replace method");
    Console.WriteLine();

    Console.WriteLine("Original value:");
    Show(sb);

    sb.Replace('#', '!', 15, 29);        // Some '#' -> '!'
    Show(sb);
    sb.Replace('!', 'o');                // All '!' -> 'o'
    Show(sb);
    sb.Replace("cat", "dog");            // All "cat" -> "dog"
    Show(sb);
    sb.Replace("dog", "fox", 15, 20);    // Some "dog" -> "fox"

    Console.WriteLine("Final value:");
    Show(sb);
    }

    public static void Show(StringBuilder sbs)
    {
    string rule1 = "0----+----1----+----2----+----3----+----4---";
    string rule2 = "01234567890123456789012345678901234567890123";

    Console.WriteLine(rule1);
    Console.WriteLine(rule2);
    Console.WriteLine("{0}", sbs.ToString());
    Console.WriteLine();
    }
}
/*
This example produces the following results:

StringBuilder.Replace method

Original value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick br!wn d#g jumps #ver the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick br!wn d!g jumps !ver the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown dog jumps over the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown dog jumps over the lazy dog.

Final value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown fox jumps over the lazy dog.

*/

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

void Show(StringBuilder* sbs)
{
    String* rule1 = S"0----+----1----+----2----+----3----+----4---";
    String* rule2 = S"01234567890123456789012345678901234567890123";

    Console::WriteLine(rule1);
    Console::WriteLine(rule2);
    Console::WriteLine(S"{0}",sbs);
    Console::WriteLine();
}
int main() 
{
//                  0----+----1----+----2----+----3----+----4---
//                  01234567890123456789012345678901234567890123
    String* str = S"The quick br!wn d#g jumps #ver the lazy cat.";
    StringBuilder* sb = new StringBuilder(str);

    Console::WriteLine();
    Console::WriteLine(S"StringBuilder.Replace method");
    Console::WriteLine();

    Console::WriteLine(S"Original value:");
    Show(sb);

    sb->Replace('#', '!', 15, 29);         // Some '#' -> '!'
    Show(sb);
    sb->Replace('!', 'o');                 // All '!' -> 'o'
    Show(sb);
    sb->Replace(S"cat", S"dog");           // All "cat" -> "dog"
    Show(sb);
    sb->Replace(S"dog", S"fox", 15, 20);   // Some "dog" -> "fox"

    Console::WriteLine(S"Final value:");
    Show(sb);
}
/*
This example produces the following results:

StringBuilder.Replace method

Original value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick br!wn d#g jumps #ver the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick br!wn d!g jumps !ver the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown dog jumps over the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown dog jumps over the lazy dog.

Final value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown fox jumps over the lazy dog.

*/

[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.Replace オーバーロードの一覧 | Remove