C# String Replace(String, String)

Description

String Replace(String, String) returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

Syntax

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


public string Replace(
  string oldValue,
  string newValue
)

Parameters

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

  • oldValue - The string to be replaced.
  • newValue - The string to replace all occurrences of oldValue.

Returns

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

Example

The following example demonstrates how you can use the Replace method to correct a spelling error.


/*  w w w. j  a  v a  2  s  .  co  m*/
using System;

public class ReplaceTest {
    public static void Main() {

        string errString = "this is a test";

        string correctString = errString.Replace("t", "d");

        Console.WriteLine(correctString);
    }
}

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