StringBuilder.Replace メソッド (Char, Char, Int32, Int32)
このインスタンスの部分文字列に出現する指定文字をすべて、別に指定した文字に置換します。
Overloads Public Function Replace( _
ByVal oldChar As Char, _ ByVal newChar As Char, _ ByVal startIndex As Integer, _ ByVal count As Integer _) As StringBuilder
[C#]
public StringBuilder Replace(charoldChar,charnewChar,intstartIndex,intcount);
[C++]
public: StringBuilder* Replace(__wchar_toldChar,__wchar_tnewChar,intstartIndex,intcount);
[JScript]
public function Replace(
oldChar : Char,newChar : Char,startIndex : int,count : int) : StringBuilder;
パラメータ
- oldChar
置換する文字。 - newChar
oldChar を置換する文字。 - startIndex
部分文字列の開始位置。 - count
部分文字列の長さ。
戻り値
startIndex から startIndex + count-1 までの範囲内で、 oldChar が newChar に置換されたこのインスタンスへの参照。
例外
例外の種類 | 条件 |
---|---|
ArgumentOutOfRangeException | startIndex に count を加算した値が、このインスタンスの値の長さを超えています。
または startIndex または count が 0 未満です。 |
解説
文字が置換されただけのため、 StringBuilder のサイズは変更されません。このメソッドでは、大文字と小文字が区別されます。
使用例
' 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 オーバーロードの一覧