Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

StringBuilder.Replace Method (Char, Char, Int32, Int32)

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

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)
public StringBuilder Replace(
	char oldChar,
	char newChar,
	int startIndex,
	int count
)

Parameters

oldChar
Type: System.Char
The character to replace.
newChar
Type: System.Char
The character that replaces oldChar.
startIndex
Type: System.Int32
The position in this instance where the substring begins.
count
Type: System.Int32
The length of the substring.

Return Value

Type: System.Text.StringBuilder
A reference to this instance with oldChar replaced by newChar in the range from startIndex to startIndex + count -1.
ExceptionCondition
ArgumentOutOfRangeException

startIndex + count is greater than the length of the value of this instance.

-or-

startIndex or count is less than zero.

The size of the StringBuilder is unchanged because characters are only replaced. This method is case-sensitive.

The following code example demonstrates the Replace method.


using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      //                0----+----1----+----2----+----3----+----4---
      //                01234567890123456789012345678901234567890123
      string str = "The quick br!wn d#g jumps #ver the lazy cat.";
      StringBuilder sb = new StringBuilder(str);

      outputBlock.Text += "\n";
      outputBlock.Text += "StringBuilder.Replace method" + "\n";
      outputBlock.Text += "\n";

      outputBlock.Text += "Original value:" + "\n";
      Show(outputBlock, sb);

      sb.Replace('#', '!', 15, 29);        // Some '#' -> '!'
      Show(outputBlock, sb);
      sb.Replace('!', 'o');                // All '!' -> 'o'
      Show(outputBlock, sb);
      sb.Replace("cat", "dog");            // All "cat" -> "dog"
      Show(outputBlock, sb);
      sb.Replace("dog", "fox", 15, 20);    // Some "dog" -> "fox"

      outputBlock.Text += "Final value:" + "\n";
      Show(outputBlock, sb);
   }

   public static void Show(System.Windows.Controls.TextBlock outputBlock, StringBuilder sbs)
   {
      string rule1 = "0----+----1----+----2----+----3----+----4---";
      string rule2 = "01234567890123456789012345678901234567890123";

      outputBlock.Text += rule1 + "\n";
      outputBlock.Text += rule2 + "\n";
      outputBlock.Text += String.Format("{0}", sbs.ToString()) + "\n";
      outputBlock.Text += "\n";
   }
}
/*
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.

*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.