String.Replaceとは? わかりやすく解説

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

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function Replace ( _
    oldChar As Char, _
    newChar As Char _
) As String
Dim instance As String
Dim oldChar As Char
Dim newChar As Char
Dim returnValue As String

returnValue = instance.Replace(oldChar, newChar)
public string Replace (
    char oldChar,
    char newChar
)
public:
String^ Replace (
    wchar_t oldChar, 
    wchar_t newChar
)
public String Replace (
    char oldChar, 
    char newChar
)
public function Replace (
    oldChar : char, 
    newChar : char
) : String

パラメータ

oldChar

置換する Unicode 文字。

newChar

出現するすべての oldChar を置換する Unicode 文字。

戻り値
このインスタンスと等価であり、oldChar のすべてのインスタンスが newChar に置換されている String。

解説解説

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

使用例使用例

連続した番号の間にある空白をコンマに置き換えることで、コンマ区切りの値リストを作成するコード例を次に示します。

Imports System
 _

Class stringReplace1
   Public Shared Sub Main()
      Dim str As [String] = "1
 2 3 4 5 6 7 8 9"
      Console.WriteLine("Original string: ""{0}""",
 str)
      Console.WriteLine("CSV string:      ""{0}""",
 str.Replace(" "c, ","c))
   End Sub
End Class
'
' This example produces the following output:
' Original string: "1 2 3 4 5 6 7 8 9"
' CSV string:      "1,2,3,4,5,6,7,8,9"
'
using System;

class stringReplace1 {
    public static void Main()
 {
        String str = "1 2 3 4 5 6 7 8 9";
        Console.WriteLine("Original string: \"{0}\"",
 str);
        Console.WriteLine("CSV string:      \"{0}\"",
 str.Replace(' ', ','));
    }
}
//
// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
//
using namespace System;
int main()
{
   String^ str = "1 2 3 4 5 6 7 8 9";
   Console::WriteLine( "Original string: \"{0}\"",
 str );
   Console::WriteLine( "CSV string:      \"{0}\"",
 str->Replace( ' ', ',' ) );
}

//
// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
//
import System.*;

class StringReplace1
{
    public static void main(String[]
 args)
    {
        String str = "1 2 3 4 5 6 7 8 9";
        Console.WriteLine("Original string: \"{0}\"",
 str);
        Console.WriteLine("CSV string:      \"{0}\"",
 str.Replace(' ', ','));
    } //main
} //StringReplace1
//
// This example produces the following output:
// Original string: "1 2 3 4 5 6 7 8 9"
// CSV string:      "1,2,3,4,5,6,7,8,9"
//
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function Replace ( _
    oldValue As String, _
    newValue As String _
) As String
Dim instance As String
Dim oldValue As String
Dim newValue As String
Dim returnValue As String

returnValue = instance.Replace(oldValue, newValue)
public string Replace (
    string oldValue,
    string newValue
)
public:
String^ Replace (
    String^ oldValue, 
    String^ newValue
)
public String Replace (
    String oldValue, 
    String newValue
)
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
End Class
'
' 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'
'
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'
//
using namespace System;
int main()
{
   String^ errString = "This docment uses 3 other docments to docment the docmentation";
   Console::WriteLine( "The original string is:\n'{0}'\n",
 errString );

   // Correct the spelling of S"document".
   String^ correctString = errString->Replace( "docment", "document"
 );
   Console::WriteLine( "After correcting the string, the
 result is:\n'{0}'", 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();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Replace メソッド




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「String.Replace」の関連用語

String.Replaceのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.Replaceのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS