SkillAgentSearch skills...

Caseless

Change string comparisons to be case insensitive.

Install / Use

/learn @Fody/Caseless
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<img src="/package_icon.png" height="30px"> Caseless.Fody

NuGet Status

Change string comparisons to be case insensitive.

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet installation

Install the Caseless.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package Caseless.Fody

The Install-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

<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_equality
  • Inequality http://msdn.microsoft.com/en-us/library/system.string.op_inequality
  • Equals(String) http://msdn.microsoft.com/en-us/library/858x0yyx
  • Equals(String, String) http://msdn.microsoft.com/en-us/library/1hkt4325
  • Compare(String,String) http://msdn.microsoft.com/en-us/library/84787k22
  • CompareTo(String) http://msdn.microsoft.com/en-us/library/35f0x18w
  • EndsWith(String) http://msdn.microsoft.com/en-us/library/2333wewz
  • Contains(String) http://msdn.microsoft.com/en-us/library/dy85x1sa
  • IndexOf(String) http://msdn.microsoft.com/en-us/library/k8b1470s
  • IndexOf(String, Int) http://msdn.microsoft.com/en-us/library/7cct0x33
  • IndexOf(String, Int, Int) http://msdn.microsoft.com/en-us/library/d93tkzah
  • LastIndexOf(String) http://msdn.microsoft.com/en-us/library/1wdsy8fy
  • LastIndexOf(String, Int) http://msdn.microsoft.com/en-us/library/bc3z4t9d
  • LastIndexOf(String, Int, Int) http://msdn.microsoft.com/en-us/library/d0z3tk9t
  • StartsWith(String) http://msdn.microsoft.com/en-us/library/baketfxw

What about String.Replace

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. Taken 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

The default string comparison can be configured in the FodyWeavers.xml file.

For example to use StringComparison.InvariantCultureIgnoreCase add the following to FodyWeavers.xml.

<Weavers>
    <Caseless StringComparison="InvariantCultureIgnoreCase"/>
</Weavers>

Icon

<a href="https://thenounproject.com/noun/aardvark/#icon-No6982">Aardvark</a> designed by <a href="https://thenounproject.com/nmac">N J MacNeil</a> from The Noun Project.

Related Skills

View on GitHub
GitHub Stars41
CategoryDevelopment
Updated2mo ago
Forks9

Languages

C#

Security Score

90/100

Audited on Jan 16, 2026

No findings