C# StringBuilder Replace(Char, Char)

Description

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

Syntax

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


public StringBuilder Replace(
  char oldChar,
  char newChar
)

Parameters

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

  • oldChar - The character to replace.
  • newChar - The character that replaces oldChar.

Returns

StringBuilder.Replace(Char, Char) method returns A reference to this instance with oldChar replaced by newChar.

Example

The following example demonstrates the Replace method.


/*  w  ww .  ja v  a2  s.  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('#', '!', 15, 29);        // Some '#' -> '!'
        Console.WriteLine("{0}", sb.ToString());

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding