String.Replace Method (Char, Char)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Function Replace ( _
oldChar As Char, _
newChar As Char _
) As String
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.
Remarks
This method performs an ordinal (case-sensitive and culture-insensitive) search to find oldChar.
Note: |
---|
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. |
Examples
The following example creates a comma-separated value list by substituting commas for the blanks between a series of numbers.
Class Example
Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
Dim str As [String] = "1 2 3 4 5 6 7 8 9"
outputBlock.Text += String.Format("Original string: ""{0}""", str) & vbCrLf
outputBlock.Text += String.Format("CSV string: ""{0}""", str.Replace(" "c, ","c)) & vbCrLf
End Sub
End Class
' 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"
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"
//
Version Information
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
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also