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

Description

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

Syntax

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


public StringBuilder Replace(
  string oldValue,//from  w w  w .  j a va2  s  .c o  m
  string newValue,
  int startIndex,
  int count
)

Parameters

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

  • oldValue - The string to replace.
  • newValue - The string that replaces oldValue, or null.
  • startIndex - The position in this instance where the substring begins.
  • count - The length of the substring.

Returns

StringBuilder.Replace(String, String, Int32, Int32) method returns A reference to this instance with all instances of oldValue replaced by newValue in the range from startIndex to startIndex + count - 1.

Example

The following example demonstrates the Replace method.


//from   ww  w.  j av a 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("dog", "fox", 15, 20);    // Some "dog" -> "fox"
        Console.WriteLine("{0}", sb.ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding