Export (0) Print
Expand All
6 out of 14 rated this helpful - Rate this topic

String.Replace Method

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

Overload List

Replaces all occurrences of a specified Unicode character in this instance with another specified Unicode character.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(Char, Char) As String
[C#] public string Replace(char, char);
[C++] public: String* Replace(__wchar_t, __wchar_t);
[JScript] public function Replace(Char, Char) : String;

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, String) As String
[C#] public string Replace(string, string);
[C++] public: String* Replace(String*, String*);
[JScript] public function Replace(String, String) : String;

Example

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

[Visual Basic] 
Imports System

Public Class ReplaceTest
    
    Public Shared Sub Main()
        Dim errString As 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".  
        Dim correctString As String = errString.Replace("docment", "document")
      
        Console.WriteLine("After correcting the string, the result is:{0}'{1}'", Environment.NewLine, correctString)
    End Sub 'Main
End Class 'ReplaceTest

[C#] 
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);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

int main()
{
   String* errString = S"This docment uses 3 other docments to docment the docmentation";

   Console::WriteLine(S"The original string is:\n'{0}'\n", errString);

   // Correct the spelling of S"document".

   String* correctString = errString->Replace(S"docment", S"document");

   Console::WriteLine(S"After correcting the string, the result is:\n'{0}'", 
      correctString);
}

[JScript] 
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();

See Also

String Class | String Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.