C# String Replace(Char, Char)

Description

String Replace(Char, Char) returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character.

Syntax

String.Replace(Char, Char) has the following syntax.


public string Replace(
  char oldChar,
  char newChar
)

Parameters

String.Replace(Char, Char) has the following parameters.

  • oldChar - The Unicode character to be replaced.
  • newChar - The Unicode character to replace all occurrences of oldChar.

Returns

String.Replace(Char, Char) method returns A string that is equivalent to this instance except that all instances of oldChar are replaced with newChar. If oldChar is not found in the current instance, the method returns the current instance unchanged.

Example

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


using System;/* w  ww .j av a  2 s.  com*/

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

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version