StringBuilder.Replace Method (Char, Char, Int32, Int32)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Replaces, within a substring of this instance, all occurrences of a specified character with another specified character.
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Function Replace ( _ oldChar As Char, _ newChar As Char, _ startIndex As Integer, _ count As Integer _ ) As StringBuilder
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.StringBuilderA reference to this instance with oldChar replaced by newChar in the range from startIndex to startIndex + count -1.
| Exception | Condition |
|---|---|
| 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.
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. '