StringBuilder::Replace Method (Char, Char, Int32, Int32)
Replaces, within a substring of this instance, all occurrences of a specified character with another specified character.
Assembly: mscorlib (in mscorlib.dll)
public: StringBuilder^ Replace( wchar_t oldChar, wchar_t 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.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | startIndex + count is greater than the length of the value of this instance. -or- startIndex or count is less than zero. |
This method performs an ordinal, case-sensitive comparison to identify occurrences of oldChar in the current instance. The size of the current StringBuilder object is unchanged after the replacement.
The following example demonstrates the Replace method.
using namespace System; using namespace System::Text; void Show( StringBuilder^ sbs ) { String^ rule1 = "0----+----1----+----2----+----3----+----4---"; String^ rule2 = "01234567890123456789012345678901234567890123"; Console::WriteLine( rule1 ); Console::WriteLine( rule2 ); Console::WriteLine( "{0}", sbs ); Console::WriteLine(); } int main() { // 0----+----1----+----2----+----3----+----4--- // 01234567890123456789012345678901234567890123 String^ str = "The quick br!wn d#g jumps #ver the lazy cat."; StringBuilder^ sb = gcnew StringBuilder( str ); Console::WriteLine(); Console::WriteLine( "StringBuilder.Replace method" ); Console::WriteLine(); Console::WriteLine( "Original value:" ); Show( sb ); sb->Replace( '#', '!', 15, 29 ); // Some '#' -> '!' Show( sb ); sb->Replace( '!', 'o' ); // All '!' -> 'o' Show( sb ); sb->Replace( "cat", "dog" ); // All "cat" -> "dog" Show( sb ); sb->Replace( "dog", "fox", 15, 20 ); // Some "dog" -> "fox" Console::WriteLine( "Final value:" ); Show( sb ); } /* 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. */
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1