String.Replace Method (Char, Char)

July 28, 2014

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"
//


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft