C# | Replace() メソッド
C#では、 交換する() メソッドは文字列メソッドです。このメソッドは、現在の文字列オブジェクトから指定されたすべての Unicode 文字または指定された文字列を置き換え、新しい変更された文字列を返すために使用されます。このメソッドは、引数を渡すことでオーバーロードできます。
構文:
public string Replace(char Oldchar, char Newchar) or public string Replace(string Oldvalue, string Newvalue)
説明:
最初のメソッドは、Oldchar と Newchar という 2 つのパラメータを受け取ります。Oldchar は置換される Unicode 文字、Newchar はすべての OldChar を置換する文字です。
2 番目のメソッドは、Oldvalue と Newvalue という 2 つのパラメーターも受け取ります。Oldvalue は置換される文字列で、Newvalue はすべての Oldvalue を置換する文字列です。両方のメソッドの戻り値の型の値は次のとおりです。 System.String 。
例外:
- ArgumentNullException : OldValue または Oldchar が両方とも null の場合。 ArgumentException OldValue または Oldchar が空の文字列 () の場合。
以下は、上記の方法を示すプログラムです。
- 例 1: をデモンストレーションするプログラム パブリック文字列 Replace(char Oldchar, char Newchar) 方法。指定された文字が出現するすべてが、別の指定された文字に置き換えられます。現在の文字列オブジェクトに oldChar が見つからない場合、文字列は変更されないままになります。
Input : str = 'GeeksForGeeks' str.Replace('s', 'G'); Output: GeekGForGeekG Input : str = 'GeeksForGeeks' str.Replace('e', ' '); Output: G ksForG ks
// C# program to illustrate the Replace()> // Method with character parameter> using> System;> > class> Geeks {> > > // Main Method> > public> static> void> Main()> > {> > > // string> > String str => 'Geeks For Geeks'> ;> > > Console.WriteLine(> 'OldString : '> + str);> > > // replace the character 's' with 'G'> > Console.WriteLine(> 'NewString: '> + str.Replace(> 's'> ,> 'G'> ));> > > // oldString will remain unchanged> > // its return the modified string> > Console.WriteLine(> '
OldString: '> + str);> > > // replace the character 'e' with space ' '> > Console.WriteLine(> 'NewString: '> + str.Replace(> 'e'> ,> ' '> ));> > }> }> |
出力:
OldString : Geeks For Geeks NewString: GeekG For GeekG OldString: Geeks For Geeks NewString: G ks For G ks例 2: をデモンストレーションするプログラム public string Replace(string Oldvalue, string Newvalue) 方法。現在の文字列インスタンス内に出現する指定された文字列はすべて、別の指定された文字列に置き換えられます。現在の文字列に Oldvalue が見つからない場合、文字列は変更されないままになります。
Input: str = 'Geeks For Geeks' str.Replace('Geeks', '---'); Output: --- For --- Input: str = 'Geeks For Geeks' str.Replace('For', 'GFG'); Output: Geeks GFG Geeks
// C# program to illustrate the Replace> // Method with string parameter> using> System;> > class> Geeks {> > > // Main Method> > public> static> void> Main()> > {> > > // define string> > String str => 'Geeks For Geeks'> ;> > > Console.WriteLine(> 'OldString : '> + str);> > > // replace the string 'Geeks' with '---'> > // in string 'Geeks comes two time so replace two times> > Console.WriteLine(> 'NewString: '> + str.Replace(> 'Geeks'> ,> '---'> ));> > > // oldString will remain unchanged> > // its return the modified string> > Console.WriteLine(> '
OldString: '> + str);> > > // replace the string 'For' with 'GFG'> > Console.WriteLine(> 'NewString: '> + str.Replace(> 'For'> ,> 'GFG'> ));> > }> }> |
出力:
OldString : Geeks For Geeks NewString: --- For --- OldString: Geeks For Geeks NewString: Geeks GFG Geeks
文字列(置換のチェーン)に対して複数の置換操作を実行するには:
上記の Replace() メソッドは変更された文字列を返すため、Replace メソッドへの連続した呼び出しを連鎖させて、文字列に対して複数の置換を実行できるようになります。メソッド呼び出しは左から右に実行されます。
以下の例では、指定された文字列 XXXXX の場合、最初に X が Y に置き換えられ、次に Y が Z に置き換えられ、最後に Z が A に置き換えられます。
例 :
// C# program to demonstrate the> // multiple replacements calls> using> System;> > public> class> Geeks{> > > // Main Method> > public> static> void> Main()> > {> > String str => 'XXXXX'> ;> > Console.WriteLine(> 'Old String: '> + str);> > > // chain together> > str = str.Replace(> 'X'> ,> 'Y'> ).Replace(> 'Y'> ,> 'Z'> ).Replace(> 'Z'> ,> 'A'> );> > Console.WriteLine(> 'New string: '> + str);> > }> }> |
出力:
Old String: XXXXX New string: AAAAA
覚えておくべき重要な点:
- Replace() メソッドは、現在のインスタンスの値を変更しません。代わりに、Oldvalue がすべて Newvalue に置き換えられた新しい文字列が返されます。同様に、oldchar は Newchar に置き換えられます。
- 大文字と小文字を区別した検索を実行して、OldValue または Oldchar を見つけます。 Newvalue が null の場合、Oldvalue はすべて削除されます。
参考文献:
- https://msdn.microsoft.com/en-us/library/czx8s9ts(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx