Export (0) Print
Expand All

String.Replace Method (Char, Char)

Returns a new string in which all occurrences of a specified Unicode character in the current string are replaced with another specified Unicode character.

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

public string Replace(
	char oldChar,
	char newChar
)

Parameters

oldChar
Type: System.Char
A Unicode character to be replaced.
newChar
Type: System.Char
A Unicode character to replace all occurrences of oldChar.

Return Value

Type: System.String
A string that is equivalent to this instance except that all instances of oldChar are replaced with newChar.

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

NoteNote:

This method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of oldChar are replaced by newChar.

The following example creates a comma-separated value list by substituting commas for the blanks between a series of numbers.


using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      String str = "1 2 3 4 5 6 7 8 9";
      outputBlock.Text += String.Format("Original string: \"{0}\"", str) + "\n";
      outputBlock.Text += String.Format("CSV string:      \"{0}\"", str.Replace(' ', ',')) + "\n";
   }
}
//
// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
//


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

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

Community Additions

ADD
Show:
© 2014 Microsoft