Поделиться через


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function Replace ( _
    oldValue As String, _
    newValue As String, _
    startIndex As Integer, _
    count As Integer _
) As StringBuilder
public StringBuilder Replace(
    string oldValue,
    string newValue,
    int startIndex,
    int count
)

Parameters

  • newValue
    Type: System.String
    The string that replaces oldValue, or nulla null reference (Nothing in Visual Basic).
  • startIndex
    Type: System.Int32
    The position in this instance where the substring begins.

Return Value

Type: System.Text.StringBuilder
A reference to this instance with all instances of oldValue replaced by newValue in the range from startIndex to startIndex + count - 1.

Exceptions

Exception Condition
ArgumentNullException

oldValue is nulla null reference (Nothing in Visual Basic).

ArgumentException

The length of oldvalue is zero.

ArgumentOutOfRangeException

startIndex or count is less than zero.

-or-

startIndex plus count indicates a character position not within this instance.

-or-

Enlarging the value of this instance would exceed its maximum capacity.

Remarks

The strings to replace are checked on an ordinal basis; that is, the replacement is not culture-aware. If newValue is nulla null reference (Nothing in Visual Basic), all occurrences of oldValue are removed. This method is case-sensitive.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 If you call this method when a null character exists in the string, the new string will be truncated at the first null character.

Examples

The following code example demonstrates the Replace method.

Imports System.Text

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      '                    0----+----1----+----2----+----3----+----4---
      '                    01234567890123456789012345678901234567890123
      Dim str As String = "The quick br!wn d#g jumps #ver the lazy cat."
      Dim sb As New StringBuilder(str)

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= "StringBuilder.Replace method" & vbCrLf
      outputBlock.Text &= vbCrLf

      outputBlock.Text &= "Original value:" & vbCrLf
      Show(outputBlock, sb)

      sb.Replace("#"c, "!"c, 15, 29)   ' Some '#' -> '!'
      Show(outputBlock, sb)
      sb.Replace("!"c, "o"c)           ' 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:" & vbCrLf
      Show(outputBlock, sb)
   End Sub 'Main

   Public Shared Sub Show(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal sbs As StringBuilder)
      Dim rule1 As String = "0----+----1----+----2----+----3----+----4---"
      Dim rule2 As String = "01234567890123456789012345678901234567890123"

      outputBlock.Text &= rule1 & vbCrLf
      outputBlock.Text &= rule2 & vbCrLf
      outputBlock.Text += String.Format("{0}", sbs.ToString()) & vbCrLf
      outputBlock.Text &= vbCrLf
   End Sub 'Show
End Class 'Sample
'
'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.
'
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.

*/

Version Information

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

Platforms

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