以下の内容はhttps://baba-s.hatenablog.com/entry/2023/01/25/134558より取得しました。


【C#】最初に見つかった文字列だけ置換する string.Replace

ソースコード

public static string ReplaceFirst
(
    this string self,
    string      oldValue,
    string      newValue
)
{
    var startIndex = self.IndexOf( oldValue );

    if ( startIndex == -1 ) return self;

    return self
            .Remove( startIndex, oldValue.Length )
            .Insert( startIndex, newValue )
        ;
}

public static string ReplaceFirst
(
    this string      self,
    string           oldValue,
    string           newValue,
    StringComparison comparisonType
)
{
    var startIndex = self.IndexOf( oldValue, comparisonType );

    if ( startIndex == -1 ) return self;

    return self
            .Remove( startIndex, oldValue.Length )
            .Insert( startIndex, newValue )
        ;
}

使用例

Debug.Log( "ギギアル".Replace( "ギ", "ギギ" ) );      // ギギギギアル
Debug.Log( "ギギアル".ReplaceFirst( "ギ", "ギギ" ) ); // ギギギアル



以上の内容はhttps://baba-s.hatenablog.com/entry/2023/01/25/134558より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14