StringBuilder.Replace 方法

定義

以另一個指定的字元或字串,取代這個執行個體中指定的字元或字串的所有項目。

多載

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 的字元。

傳回

StringBuilder

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

傳回

StringBuilder

這個執行個體的參考,但 oldValue 的所有執行個體遭取代成 newValue

例外狀況

oldValuenull

oldValue 的長度為零。

加大此執行個體的值可能會超過 MaxCapacity

備註

這個方法會執行序數、區分大小寫的比較,以識別 oldValue 目前實例中出現的專案。 如果 newValuenullString.EmptyoldValue 則會移除所有出現的。

另請參閱

適用於

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

子字串的長度。

傳回

StringBuilder

參考這個執行個體,但 oldChar 換成 newChar,範圍是 startIndexstartIndex + count -1。

例外狀況

startIndex + count 大於這個執行個體值的長度。

-或-

startIndexcount 小於零。

備註

這個方法會執行序數、區分大小寫的比較,以識別 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

子字串的長度。

傳回

StringBuilder

參考這個執行個體,但 oldValue 的所有執行個體換成 newValue,範圍是 startIndexstartIndex + count - 1。

例外狀況

oldValuenull

oldValue 的長度為零。

startIndexcount 小於零。

-或-

startIndex 加上 count 表示不在此執行個體中的字元位置。

-或-

加大此執行個體的值可能會超過 MaxCapacity

備註

這個方法會執行序數、區分大小寫的比較,以識別 oldValue 指定子字串中的出現次數。 如果 newValuenullString.EmptyoldValue 則會移除所有出現的。

另請參閱

適用於