String.Replace Method (Char, Char)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns a new string in which all occurrences of a specified Unicode character in the current string are replaced with another specified Unicode character.
Assembly: mscorlib (in mscorlib.dll)
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.StringA string that is equivalent to this instance except that all instances of oldChar are replaced with 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" //
Show:
Note: