This documentation is archived and is not being maintained.

String.Replace Method (Char, Char)

.NET Framework 1.1

Replaces all occurrences of a specified Unicode character in this instance with another specified Unicode character.

[Visual Basic]
Overloads Public Function Replace( _
   ByVal oldChar As Char, _
   ByVal newChar As Char _
) As String
[C#]
public string Replace(
 char oldChar,
 char newChar
);
[C++]
public: String* Replace(
 __wchar_t oldChar,
 __wchar_t newChar
);
[JScript]
public function Replace(
   oldChar : Char,
 newChar : Char
) : String;

Parameters

oldChar
A Unicode character to be replaced.
newChar
A Unicode character to replace all occurrences of oldChar.

Return Value

A String equivalent to this instance but with all instances of oldChar replaced with newChar.

Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) search to find oldChar.

Example

[Visual Basic, C#, C++] The following example creates a comma separated value list by substituting commas for the blanks between a series of numbers.

[Visual Basic] 
Imports System
 _

Class stringReplace1
   Public Shared Sub Main()
      Dim str As [String] = "1 2 3 4 5 6 7 8 9"
      Console.WriteLine("Original string: ""{0}""", str)
      Console.WriteLine("CSV string:      ""{0}""", str.Replace(" "c, ","c))
   End Sub 'Main
End Class 'stringReplace1

[C#] 
using System;

class stringReplace1 {
    public static void Main() {
        String str = "1 2 3 4 5 6 7 8 9";
        Console.WriteLine("Original string: \"{0}\"", str);
        Console.WriteLine("CSV string:      \"{0}\"", str.Replace(' ', ','));
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

int main()
{
   String* str = S"1 2 3 4 5 6 7 8 9";
   Console::WriteLine(S"Original string: \"{0}\"", str);
   Console::WriteLine(S"CSV string:      \"{0}\"", str->Replace(' ', ','));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.Replace Overload List | Char | Concat | Insert | Join | Remove | Split | Substring | Trim

Show: