Created
September 22, 2023 13:53
-
-
Save SteGriff/6c1efa24b8600d06ab7af8d20938f783 to your computer and use it in GitHub Desktop.
C# String ReplaceFirst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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