Added string.ReplaceFirst extension method.

pull/625/head
Halil ibrahim Kalkan 7 years ago
parent cd01234f00
commit 2a784037f5

@ -192,6 +192,19 @@ namespace System
return str;
}
public static string ReplaceFirst(this string str, string search, string replace, StringComparison comparisonType = StringComparison.Ordinal)
{
Check.NotNull(str, nameof(str));
var pos = str.IndexOf(search, comparisonType);
if (pos < 0)
{
return str;
}
return str.Substring(0, pos) + replace + str.Substring(pos + search.Length);
}
/// <summary>
/// Gets a substring of a string from end of the string.
/// </summary>

@ -187,6 +187,14 @@ namespace System
"Https://abp.io".RemovePreFix(StringComparison.OrdinalIgnoreCase, "https://").ShouldBe("abp.io");
}
[Fact]
public void ReplaceFirst_Tests()
{
"Test string".ReplaceFirst("s", "X").ShouldBe("TeXt string");
"Test test test".ReplaceFirst("test", "XX").ShouldBe("Test XX test");
"Test test test".ReplaceFirst("test", "XX", StringComparison.OrdinalIgnoreCase).ShouldBe("XX test test");
}
[Fact]
public void ToEnum_Test()
{

Loading…
Cancel
Save