Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 923e666

Browse files
committed
Fix ReplaceAll
1 parent fee0f67 commit 923e666

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,9 @@ public static string ReplaceAll(this string haystack, string needle, string repl
913913
{
914914
int pos;
915915
// Avoid a possible infinite loop
916-
if (needle == replacement) return haystack;
917-
while ((pos = haystack.IndexOf(needle, StringComparison.Ordinal)) > 0)
916+
if (needle == replacement)
917+
return haystack;
918+
while ((pos = haystack.IndexOf(needle, StringComparison.Ordinal)) >= 0)
918919
{
919920
haystack = haystack.Substring(0, pos)
920921
+ replacement

tests/ServiceStack.Text.Tests/StringExtensionsTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,5 +355,12 @@ public void Does_ContainsAny_Return_CaseInsensitive_Matches()
355355

356356
Assert.That(input.ContainsAny(testMatches, StringComparison.OrdinalIgnoreCase));
357357
}
358+
359+
[Test]
360+
public void Does_ReplaceAll_from_Start()
361+
{
362+
Assert.That("/images".ReplaceAll("/",""), Is.EqualTo("images"));
363+
}
364+
358365
}
359366
}

0 commit comments

Comments
 (0)