How do I replace the *first instance* of a string in .NET?

后端 未结 14 1070
忘掉有多难
忘掉有多难 2020-11-22 16:41

I want to replace the first occurrence in a given string.

How can I accomplish this in .NET?

14条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 16:59

    In C# syntax:

    int loc = original.IndexOf(oldValue);
    if( loc < 0 ) {
        return original;
    }
    return original.Remove(loc, oldValue.Length).Insert(loc, newValue);
    

提交回复
热议问题