String.Replace Method (String, String)
Assembly: mscorlib (in mscorlib.dll)
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.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.
<#
.SYNOPSIS
This script demonstrates how to do .REPLACE() in a string.
.DESCRIPTION
This script is a MSDN sample, recoded in PowerShell
.NOTES
File Name : Replace-String.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/fk49wtc1%28VS.80%29.aspx
.EXAMPLE
PSH [C:\foo]: .\Replace-String.ps1
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
#>
##
# Start of script
##
# Create a string complete with misspelling and display it
$errString = "This docment uses 3 other docments to docment the docmentation"
"The original string is:`n{0}" -f $errString
""
# Correct the spelling of "document" using .Replace() and display
# the corrected string
$correctString = $errString.Replace("docment", "document")
"After correcting the string, the result is: `n{0}" -f $correctstring
# End of script
- 6/1/2010
- Thomas Lee
<#
.SYNOPSIS
This script demonstrates how to do .REPLACE() in a string.
.DESCRIPTION
This script is a MSDN sample, recoded in PowerShell
.NOTES
File Name : Replace-String.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/fk49wtc1%28VS.80%29.aspx
.EXAMPLE
.PARAMETER bar
#>
##
# Start of script
##
# Create a string complete with misspelling
# And display the string
$errString = "This docment uses 3 other docments to docment the docmentation"
"The original string is:`n{0}" -f $errString
# Correct the spelling of "document" using .Replace()
# and display the corrected string.
$correctString = $errString.Replace("docment", "document")
"After correcting the string, the result is: `n{0}" -f $correctstring
# End of script
- 6/1/2010
- Thomas Lee