StringBuilder.Replace メソッド
このインスタンスに出現する指定文字または指定文字列をすべて、別に指定した文字または文字列に置換します。
オーバーロードの一覧
このインスタンスに出現する指定文字をすべて、別に指定した文字に置換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function Replace(Char, Char) As StringBuilder
[JScript] public function Replace(Char, Char) : StringBuilder;
このインスタンスに出現する指定部分文字列をすべて、別に指定した文字列に置換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function Replace(String, String) As StringBuilder
[JScript] public function Replace(String, String) : StringBuilder;
このインスタンスの部分文字列に出現する指定文字をすべて、別に指定した文字に置換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function Replace(Char, Char, Integer, Integer) As StringBuilder
[C++] public: StringBuilder* Replace(__wchar_t, __wchar_t, int, int);
[JScript] public function Replace(Char, Char, int, int) : StringBuilder;
このインスタンスの部分文字列に出現する指定文字列をすべて、別に指定した文字列に置換します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function Replace(String, String, Integer, Integer) As StringBuilder
[C#] public StringBuilder Replace(string, string, int, int);
[C++] public: StringBuilder* Replace(String*, String*, int, int);
[JScript] public function Replace(String, String, int, int) : StringBuilder;
使用例
[Visual Basic, C#, C++] メモ ここでは、Replace のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
' 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。