Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Replace Method (String, String)

Replaces all occurrences of a specified String in this instance, with another specified String.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public string Replace (
	string oldValue,
	string newValue
)
public String Replace (
	String oldValue, 
	String newValue
)
public function Replace (
	oldValue : String, 
	newValue : String
) : String
Not applicable.

Parameters

oldValue

A String to be replaced.

newValue

A String to replace all occurrences of oldValue.

Return Value

A String equivalent to this instance but with all instances of oldValue replaced with newValue.

Exception typeCondition

ArgumentNullException

oldValue is a null reference (Nothing in Visual Basic).

ArgumentException

oldValue is the empty string ("").

If newValue is a null reference (Nothing in Visual Basic), all occurrences of oldValue are removed.

This method performs an ordinal (case-sensitive and culture-insensitive) search to find oldValue.

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

using System;

public class ReplaceTest {
    public static void Main() {

        string errString = "This docment uses 3 other docments to docment the docmentation";

        Console.WriteLine("The original string is:{0}'{1}'{0}", Environment.NewLine, errString);

        // Correct the spelling of "document".

        string correctString = errString.Replace("docment", "document");

        Console.WriteLine("After correcting the string, the result is:{0}'{1}'",
                Environment.NewLine, correctString);
    }
}
//
// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//

import System.*;

public class ReplaceTest
{
    public static void main(String[] args)
    {
        String errString = "This docment uses 3 other docments to docment "
            + "the docmentation";

        Console.WriteLine("The original string is:{0}'{1}'{0}", 
            Environment.get_NewLine(), errString);

        // Correct the spelling of "document".
        String correctString = errString.Replace("docment", "document");
        Console.WriteLine("After correcting the string, the result is:{0}'{1}'",
            Environment.get_NewLine(), correctString);
    } 
} 
//
// This code example produces the following output:
//
// The original string is:
// 'This docment uses 3 other docments to docment the docmentation'
//
// After correcting the string, the result is:
// 'This document uses 3 other documents to document the documentation'
//

import System;

public class ReplaceTest {
    public static function Main() : void {

        var errString : String = "This docment uses 3 other docments to docment the docmentation";
 
        Console.WriteLine("The original string is:{0}'{1}'{0}", Environment.NewLine, errString);

        // Correct the spelling of "document".

        var correctString : String = errString.Replace("docment", "document");

        Console.WriteLine("After correcting the string, the result is:{0}'{1}'", 
                Environment.NewLine, correctString);
    }
}
ReplaceTest.Main();

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft