StringBuilder.Replace 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
以另一個指定的字元或字串,取代這個執行個體中指定的字元或字串的所有項目。
多載
Replace(Char, Char) |
以另一個指定的字元,取代這個執行個體中指定字元的所有項目。 |
Replace(String, String) |
將這個執行個體中所有出現的指定字串取代為另一個指定字串。 |
Replace(Char, Char, Int32, Int32) |
將這個執行個體的子字串內所有出現的指定字元,取代為另一個指定的字元。 |
Replace(String, String, Int32, Int32) |
將這個執行個體的子字串內所有出現的指定字串,取代為另一個指定的字串。 |
範例
下列範例會示範 Replace 方法。
using namespace System;
using namespace System::Text;
void Show( StringBuilder^ sbs )
{
String^ rule1 = "0----+----1----+----2----+----3----+----4---";
String^ rule2 = "01234567890123456789012345678901234567890123";
Console::WriteLine( rule1 );
Console::WriteLine( rule2 );
Console::WriteLine( "{0}", sbs );
Console::WriteLine();
}
int main()
{
// 0----+----1----+----2----+----3----+----4---
// 01234567890123456789012345678901234567890123
String^ str = "The quick br!wn d#g jumps #ver the lazy cat.";
StringBuilder^ sb = gcnew 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 );
}
/*
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.
*/
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.
*/
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
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
End Class
'
'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.
'
Replace(Char, Char)
以另一個指定的字元,取代這個執行個體中指定字元的所有項目。
public:
System::Text::StringBuilder ^ Replace(char oldChar, char newChar);
public System.Text.StringBuilder Replace (char oldChar, char newChar);
member this.Replace : char * char -> System.Text.StringBuilder
Public Function Replace (oldChar As Char, newChar As Char) As StringBuilder
參數
- oldChar
- Char
要取代的字元。
- newChar
- Char
取代 oldChar
的字元。
傳回
其 oldChar
已由 newChar
取代的執行個體參考。
備註
這個方法會執行序數、區分大小寫的比較,以識別 oldChar
目前實例中出現的專案。 取代之後,目前實例的大小 StringBuilder 未變更。
適用於
Replace(String, String)
將這個執行個體中所有出現的指定字串取代為另一個指定字串。
public:
System::Text::StringBuilder ^ Replace(System::String ^ oldValue, System::String ^ newValue);
public System.Text.StringBuilder Replace (string oldValue, string newValue);
public System.Text.StringBuilder Replace (string oldValue, string? newValue);
member this.Replace : string * string -> System.Text.StringBuilder
Public Function Replace (oldValue As String, newValue As String) As StringBuilder
參數
- oldValue
- String
要取代的字串。
- newValue
- String
取代 oldValue
的字串或 null
。
傳回
這個執行個體的參考,但 oldValue
的所有執行個體遭取代成 newValue
。
例外狀況
oldValue
為 null
。
oldValue
的長度為零。
加大此執行個體的值可能會超過 MaxCapacity。
備註
這個方法會執行序數、區分大小寫的比較,以識別 oldValue
目前實例中出現的專案。 如果 newValue
是 null
或 String.Empty , oldValue
則會移除所有出現的。
另請參閱
適用於
Replace(Char, Char, Int32, Int32)
將這個執行個體的子字串內所有出現的指定字元,取代為另一個指定的字元。
public:
System::Text::StringBuilder ^ Replace(char oldChar, char newChar, int startIndex, int count);
public System.Text.StringBuilder Replace (char oldChar, char newChar, int startIndex, int count);
member this.Replace : char * char * int * int -> System.Text.StringBuilder
Public Function Replace (oldChar As Char, newChar As Char, startIndex As Integer, count As Integer) As StringBuilder
參數
- oldChar
- Char
要取代的字元。
- newChar
- Char
取代 oldChar
的字元。
- startIndex
- Int32
這個執行個體中子字串開始的位置。
- count
- Int32
子字串的長度。
傳回
參考這個執行個體,但 oldChar
換成 newChar
,範圍是 startIndex
到 startIndex
+ count
-1。
例外狀況
備註
這個方法會執行序數、區分大小寫的比較,以識別 oldChar
目前實例中出現的專案。 取代之後,目前物件的大小 StringBuilder 不會變更。
適用於
Replace(String, String, Int32, Int32)
將這個執行個體的子字串內所有出現的指定字串,取代為另一個指定的字串。
public:
System::Text::StringBuilder ^ Replace(System::String ^ oldValue, System::String ^ newValue, int startIndex, int count);
public System.Text.StringBuilder Replace (string oldValue, string newValue, int startIndex, int count);
public System.Text.StringBuilder Replace (string oldValue, string? newValue, int startIndex, int count);
member this.Replace : string * string * int * int -> System.Text.StringBuilder
Public Function Replace (oldValue As String, newValue As String, startIndex As Integer, count As Integer) As StringBuilder
參數
- oldValue
- String
要取代的字串。
- newValue
- String
取代 oldValue
的字串或 null
。
- startIndex
- Int32
這個執行個體中子字串開始的位置。
- count
- Int32
子字串的長度。
傳回
參考這個執行個體,但 oldValue
的所有執行個體換成 newValue
,範圍是 startIndex
到 startIndex
+ count
- 1。
例外狀況
oldValue
為 null
。
oldValue
的長度為零。
備註
這個方法會執行序數、區分大小寫的比較,以識別 oldValue
指定子字串中的出現次數。 如果 newValue
是 null
或 String.Empty , oldValue
則會移除所有出現的。