C# StringBuilder Replace(Char, Char, Int32, Int32)

Description

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

Syntax

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


public StringBuilder Replace(
  char oldChar,/*from  w w w .j  a  v  a2 s .  c  om*/
  char newChar,
  int startIndex,
  int count
)

Parameters

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

  • oldChar - The character to replace.
  • newChar - The character that replaces oldChar.
  • startIndex - The position in this instance where the substring begins.
  • count - The length of the substring.

Returns

StringBuilder.Replace(Char, Char, Int32, Int32) method returns A reference to this instance with oldChar replaced by newChar in the range from startIndex to startIndex + count -1.

Example

The following example demonstrates the Replace method.


/*from  www  .  ja  v  a  2  s  .c o  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(sb.ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding