Fody
This is an add-in forChange string comparisons to be case insensitive.
Usage
See also Fody usage.
NuGet installation
Install the Caseless.Fody NuGet package and update the Fody NuGet package:
PM> Install-Package Caseless.Fody
PM> Update-Package Fody
The Update-Package Fody
is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.
Add to FodyWeavers.xml
Add <Caseless/>
to FodyWeavers.xml
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<Caseless/>
</Weavers>
Your Code
public bool Foo()
{
var x = "a";
var y = "A";
return x == y;
}
What gets compiled
public bool Foo()
{
var x = "a";
var y = "A";
return string.Equals(x, y, StringComparison.OrdinalIgnoreCase);
}
Converted Methods
The following string methods get converted to their StringComparison equivalents.
Equality
http://msdn.microsoft.com/en-us/library/system.string.op_equalityInequality
http://msdn.microsoft.com/en-us/library/system.string.op_inequalityEquals(String)
http://msdn.microsoft.com/en-us/library/858x0yyxEquals(String, String)
http://msdn.microsoft.com/en-us/library/1hkt4325Compare(String,String)
http://msdn.microsoft.com/en-us/library/84787k22CompareTo(String)
http://msdn.microsoft.com/en-us/library/35f0x18wEndsWith(String)
http://msdn.microsoft.com/en-us/library/2333wewzContains(String)
http://msdn.microsoft.com/en-us/library/dy85x1saIndexOf(String)
http://msdn.microsoft.com/en-us/library/k8b1470sIndexOf(String, Int)
http://msdn.microsoft.com/en-us/library/7cct0x33IndexOf(String, Int, Int)
http://msdn.microsoft.com/en-us/library/d93tkzahLastIndexOf(String)
http://msdn.microsoft.com/en-us/library/1wdsy8fyLastIndexOf(String, Int)
http://msdn.microsoft.com/en-us/library/bc3z4t9dLastIndexOf(String, Int, Int)
http://msdn.microsoft.com/en-us/library/d0z3tk9tStartsWith(String)
http://msdn.microsoft.com/en-us/library/baketfxw
String.Replace
What about This is because there is no overload for a case insensitive replace in the .net framework.
Here is an extension method to achieve it manually. Take from this StackOverflow answer
public static class StringExtensions
{
public static StringComparison DefaultComparison = StringComparison.OrdinalIgnoreCase;
public static string ReplaceCaseless(this string str, string oldValue, string newValue)
{
var sb = new StringBuilder();
var previousIndex = 0;
var index = str.IndexOf(oldValue, DefaultComparison);
while (index != -1)
{
sb.Append(str.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;
previousIndex = index;
index = str.IndexOf(oldValue, index, DefaultComparison);
}
sb.Append(str.Substring(previousIndex));
return sb.ToString();
}
}
Default String Comparison
If your don't want to use StringComparison.OrdinalIgnoreCase
then you can configure the addin inside the FodyWeavers.xml file.
For example if you want to use StringComparison.InvariantCultureIgnoreCase
then add the following to FodyWeavers.xml.
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<Caseless StringComparison="InvariantCultureIgnoreCase"/>
</Weavers>
Icon
Aardvark designed by N J MacNeil from The Noun Project