Export (0) Print
Expand All

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

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 a word (case-sensitive and culture-sensitive) 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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft