Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Created September 22, 2023 13:53
Show Gist options
  • Save SteGriff/6c1efa24b8600d06ab7af8d20938f783 to your computer and use it in GitHub Desktop.
Save SteGriff/6c1efa24b8600d06ab7af8d20938f783 to your computer and use it in GitHub Desktop.
C# String ReplaceFirst
public static class StringExtensions
{
public static string ReplaceFirst(this string target, string oldString, string newString)
{
int start = target.IndexOf(oldString);
if (start < 0)
return target;
int end = start + oldString.Length;
return target[..start] + newString + target[end..];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment