Export (0) Print
Expand All

StringBuilder.Replace Method

Replaces all occurrences of a specified character or string in this instance with another specified character or string.

Overload List

Replaces all occurrences of a specified character in this instance with another specified character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(Char, Char) As StringBuilder
[C#] public StringBuilder Replace(char, char);
[C++] public: StringBuilder* Replace(__wchar_t, __wchar_t);
[JScript] public function Replace(Char, Char) : StringBuilder;

Replaces all occurrences of a specified substring in this instance with another specified string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, String) As StringBuilder
[C#] public StringBuilder Replace(string, string);
[C++] public: StringBuilder* Replace(String*, String*);
[JScript] public function Replace(String, String) : StringBuilder;

Replaces all occurrences of a specified character in a substring of this instance with another specified character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(Char, Char, Integer, Integer) As StringBuilder
[C#] public StringBuilder Replace(char, char, int, int);
[C++] public: StringBuilder* Replace(__wchar_t, __wchar_t, int, int);
[JScript] public function Replace(Char, Char, int, int) : StringBuilder;

Replaces all occurrences of a specified string in a substring of this instance with another specified string.

Supported by the .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;

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Replace. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' 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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

StringBuilder Class | StringBuilder Members | System.Text Namespace

Show:
© 2014 Microsoft