Share via


String.Replace メソッド

このインスタンスに出現する指定 Unicode 文字または String をすべて、別に指定した Unicode 文字または String に置換します。

オーバーロードの一覧

このインスタンスに出現する指定 Unicode 文字をすべて、別に指定した Unicode 文字に置換します。

.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;

このインスタンスに出現する指定 String 文字をすべて、別に指定した String 文字に置換します。

.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;

使用例

Replace メソッドを使用して、スペルの間違いを訂正する方法については、次のコード例を参照してください。

 
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();

参照

String クラス | String メンバ | System 名前空間