StringBuilder::Replace Method (Char, Char)
.NET Framework (current version)
Replaces all occurrences of a specified character in this instance with another specified character.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- oldChar
-
Type:
System::Char
The character to replace.
- newChar
-
Type:
System::Char
The character that replaces oldChar.
Return Value
Type: System.Text::StringBuilder^A reference to this instance with oldChar replaced by newChar.
This method performs an ordinal, case-sensitive comparison to identify occurrences of oldChar in the current instance. The size of the current StringBuilder instance 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. */
Universal Windows Platform
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
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
Show: