diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs index fbddd31bb3e0a..715ac704703c0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs @@ -986,11 +986,13 @@ private string ReplaceCore(string oldValue, string? newValue, CompareInfo? ci, C // public string Replace(char oldChar, char newChar) { - int firstIndex; - if (oldChar == newChar || (firstIndex = IndexOf(oldChar)) < 0) - { + if (oldChar == newChar) + return this; + + int firstIndex = IndexOf(oldChar); + + if (firstIndex < 0) return this; - } int remainingLength = Length - firstIndex; string result = FastAllocateString(Length);