Giter VIP home page Giter VIP logo

caseless's Introduction

Chat on Gitter NuGet Status

This is an add-in for Fody

Icon

Change string comparisons to be case insensitive.

Introduction to Fody

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.

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

caseless's People

Contributors

eatdrinksleepcode avatar licshee avatar simoncropp avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.