C# StringBuilder Replace(String, String)

Description

StringBuilder Replace(String, String) Replaces all occurrences of a specified string in this instance with another specified string.

Syntax

StringBuilder.Replace(String, String) has the following syntax.


public StringBuilder Replace(
  string oldValue,
  string newValue
)

Parameters

StringBuilder.Replace(String, String) has the following parameters.

  • oldValue - The string to replace.
  • newValue - The string that replaces oldValue, or null.

Returns

StringBuilder.Replace(String, String) method returns A reference to this instance with all instances of oldValue replaced by newValue.

Example

The following example demonstrates the Replace method.


/* www  .j a va 2s  . co  m*/
using System;
using System.Text;

class Sample 
{
    public static void Main() 
    {
        string str = "The quick br!wn d#g jumps #ver the lazy cat.";
        StringBuilder sb = new StringBuilder(str);
    
        sb.Replace("cat", "dog");            // All "cat" -> "dog"
        Console.WriteLine(sb.ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding