次の方法で共有


String.Replace メソッド (String, String)

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

Overloads Public Function Replace( _
   ByVal oldValue As String, _   ByVal newValue As String _) As String
[C#]
public string Replace(stringoldValue,stringnewValue);
[C++]
public: String* Replace(String* oldValue,String* newValue);
[JScript]
public function Replace(
   oldValue : String,newValue : String) : String;

パラメータ

  • oldValue
    置換する String
  • newValue
    出現するすべての oldValue を置換する String

戻り値

このインスタンスと等価であり、 oldValue のすべてのインスタンスが newValue に置換されている String

例外

例外の種類 条件
ArgumentNullException oldValue が null 参照 (Visual Basic では Nothing) です。
ArgumentException oldValue が空の文字列 ("") です。

解説

newValue が null 参照 (Visual Basic では Nothing) の場合は、見つかったすべての oldValue が削除されます。

このメソッドは、単語 (大文字/小文字を区別し、カルチャに依存した) 検索を実行して、 oldValue を見つけます。

使用例

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | String.Replace オーバーロードの一覧 | Concat | Insert | Join | Remove | Split | Substring | Trim