Skip to content

Fody/Caseless

Folders and files

NameName
Last commit message
Last commit date
Sep 21, 2023
Jan 30, 2024
Mar 4, 2025
Mar 4, 2025
Mar 5, 2025
Nov 29, 2023
Mar 23, 2013
Dec 3, 2022
Sep 5, 2019
Mar 18, 2024
Sep 15, 2023
Jan 30, 2024
Sep 20, 2012
Nov 22, 2017
Jan 16, 2025

Repository files navigation

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.

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

Aardvark designed by N J MacNeil from The Noun Project.