StringBuilder.Replace 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在此情況下,將指定字元或字串的所有出現替換為另一個指定的字元或字串。
多載
| 名稱 | Description |
|---|---|
| Replace(Char, Char) |
將該實例中指定字元的所有出現替換為另一個指定字元。 |
| Replace(String, String) |
將該實例中指定字串的所有出現位置替換為另一指定字串。 |
| Replace(Char, Char, Int32, Int32) |
在此實例的子字串中,將指定字元的所有出現替換為另一個指定字元。 |
| Replace(String, String, Int32, Int32) |
在此實例的子字串中,將指定字串的所有出現替換為另一指定字串。 |
範例
以下範例示範此 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.
*/
open System.Text
let show (sbs: StringBuilder) =
let rule1 = "0----+----1----+----2----+----3----+----4---"
let rule2 = "01234567890123456789012345678901234567890123"
printfn $"{rule1}\n{rule2}\n{sbs}\n"
// 0----+----1----+----2----+----3----+----4---
// 01234567890123456789012345678901234567890123
let str = "The quick br!wn d#g jumps #ver the lazy cat."
let sb = StringBuilder str
printfn "StringBuilder.Replace method\n"
printfn "Original value:"
show sb
sb.Replace('#', '!', 15, 29) |> ignore // Some '#' -> '!'
show sb
sb.Replace('!', 'o') |> ignore // All '!' -> 'o'
show sb
sb.Replace("cat", "dog") |> ignore // All "cat" -> "dog"
show sb
sb.Replace("dog", "fox", 15, 20) |> ignore // Some "dog" -> "fox"
printfn "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.
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);
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 改為 ,範圍範圍從 startIndexcount + startIndex-1 開始。newChar
例外狀況
備註
此方法進行序數、大小寫區分比較,以識別當前實例中的 出現 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);
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
子字串的長度。
傳回
此實例中所有實例皆被替換newValue為,範圍範圍為count + startIndexstartIndex 至 - 1。oldValue
例外狀況
oldValue 是 null。
的 oldValue 長度為零。
備註
此方法進行序數、大小寫區分比較,以識別指定子字串中的 出現 oldValue 。 若 newValue 是 null 或 String.Empty,則所有 的 oldValue 出現皆被移除。