Skip to content

Instantly share code, notes, and snippets.

@Galilyou
Last active December 9, 2015 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Galilyou/00dcd0dab2d2a050c30c to your computer and use it in GitHub Desktop.
Save Galilyou/00dcd0dab2d2a050c30c to your computer and use it in GitHub Desktop.
public class StringExtensions
{
public static string Replace(this string src, string oldValue, string newValue, StringComparison comparison)
{
if(string.IsNullOrWhiteSpace(src))
return src;
// skip the loop entirely if oldValue and newValue are the same
if(string.Compare(oldValue, newValue, comparison) == 0) return src;
// i'm not sure how this will work in terms of clutural comparsion,
// this is a hack to avoid the bug reported here https://stackoverflow.com/questions/244531/is-there-an-alternative-to-string-replace-that-is-case-insensitive/13847351#comment31063745_244933
if (oldValue.Length > src.Length) return src;
var sb = new StringBuilder();
int previousIndex = 0;
int index = src.IndexOf(oldValue, comparison);
while(index != -1)
{
sb.Append(src.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;
previousIndex = index;
index = src.IndexOf(oldValue, index, comparison);
}
sb.Append(src.Substring(previousIndex));
return sb.ToString();
}
}
[TestFixture]
public class StringExtensionTests
{
[TestCase(null, "old", "new", Result = null)]
[TestCase("", "old", "new", Result = "")]
public string ReplaceCalledOnNullOrEmptyReturnsNullOrEmpty(string src, string oldValue, string newValue)
{
var result = src.Replace(oldValue, newValue, StringComparison.OrdinalIgnoreCase);
return result;
}
[Test]
public void ReplaceGivenVariedCaseStringReplacesCorrectly()
{
string s = "Hello everyone from {{RecipIenT}}";
var result = s.Replace("{{recipient}}", "John Doe", StringComparison.OrdinalIgnoreCase);
Assert.That(result, Is.EqualTo("Hello everyone from John Doe"));
}
[Test]
public void ReplaceGivenVariedCaseReplacesAllOccurences()
{
string s = "Hello everyone from {{RecipIenT}}. Allow me, {{reCIPIeNt}} to welcome you all to {{RECIpieNT}}'s party.";
var result = s.Replace("{{recipient}}", "John Doe", StringComparison.OrdinalIgnoreCase);
Assert.That(result, Is.EqualTo("Hello everyone from John Doe. Allow me, John Doe to welcome you all to John Doe's party."));
}
[Test]
public void ReplaceGivenNonExistingTermReturnsOriginalString()
{
string s = "This string doesn't inlcude the term to be removed";
var result = s.Replace("not-here", "whatever", StringComparison.OrdinalIgnoreCase);
Assert.That(result, Is.EqualTo(s));
}
[Test]
public void ReplaceWhenOldValueEqualsNewValue()
{
string s = "replace my OldValue here please. OldValue dafds fOldValue";
var result = s.Replace("OldValue", "OldValue", StringComparison.OrdinalIgnoreCase);
Assert.That(result, Is.EqualTo(s));
}
[Test]
public void ReplaceEmptyStringWithEmptyString()
{
string s = "This is a string";
string result = s.Replace("", "", StringComparison.OrdinalIgnoreCase);
Assert.That(result, Is.EqualTo(s));
}
[Test]
public void ReplaceWhenOldValueLenghtIsGreaterThanSourceReturnsSource()
{
string src = "œ";
string result = src.Replace("oe", "", StringComparison.InvariantCulture);
Assert.That(result, Is.EqualTo(src));
}
// we only really care for english, but Arabic is the only other potential possibility
[Test]
public void ReplaceWorksWithArabic()
{
string src = "الشعر في العربية شئ جميل شئ جميل ان تكتب شعر في العربية، لان شعر العربية شئ جميل";
string result = src.Replace("شعر", "نثر", StringComparison.OrdinalIgnoreCase);
Assert.That(result, Is.EqualTo("النثر في العربية شئ جميل شئ جميل ان تكتب نثر في العربية، لان نثر العربية شئ جميل"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment