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

oldValuenull 参照 (Visual Basic では Nothing) です。

ArgumentException

oldValue空の文字列 ("") です。

解説解説

newValuenull 参照 (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 メソッド




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.Replace メソッド」の関連用語

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

   

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



String.Replace メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS