string.ReplaceFirst

指定した文字列内でパターンに一致する文字列の初めに見つかった部分を、指定した文字列で置き換えます。


taiseiue | 2025-02-13

定義

名前空間: Alice.Regex
アセンブリ: Losetta.Runtime.dll
ソースコード: Alice.Regex.cs

ReplaceFirst(this string,string,string)

指定した文字列内でパターンに一致する文字列の初めに見つかった部分を、指定した文字列で置き換えます。

AliceScript
namespace Alice.Regex;
public string ReplaceFirst(this string input, string pattern, string replacement);
引数
input 置換対象を検索する文字列
pattern 置換対象を検索する正規表現のパターン
replacement 置換する文字列
戻り値
string 入力文字列の該当部分を置換文字列で置き換えた新しい文字列。ただし置換すべき部分が見つからなかった場合は入力文字列となります。

対応
AliceScript 3.0、4
AliceSister 3.0、4
Losetta 0.10、0.11

説明

このメソッドは、指定した文字列内でパターンに一致する文字列の初めに見つかった部分を、指定した文字列で置き換えます。

次の例では、Cで始まりEで終わる部分をcdeに置換します。

AliceScript
using Alice.Regex;

string text = "ABCDEFG";
string replaced = text.ReplaceFirst("C*E", "cde");

print(trimmed);
//出力: ABcdeFG