Code Monkey home page Code Monkey logo

stringthings's Introduction

tests

StringThings

Handy string extensions and utilities for .NET

This is a collection of string things I find myself writing in every project I work on. I want to get it all down in one place so I don't keep rewriting the same stuff. Maybe it can help someone else if I make it public.

Getting Started

dotnet add package StringThings

TypedString

Beat primitive obsession by making specific types of strings. Unique types allow you to do things like more easily inject different strings. TypedString implicitly casts to string so it can still be used with code you don't control that requires strings.

// type to hold a string for your database connection string
public class DbConnectionString : TypedString {
  public DbConnectionString(string value) : base(value) { }
}

// register instance with dependency injection
services.AddSingleton(new DbConnectionString("...from config..."));

// ask for the registered DbConnectionString in your app
public class HomeController(DbConnectionString connStr) {
  public ActionResult Index() {
    var conn = new SqlConnection(connStr);   
  }
}

Case-insensitive Comparison Extensions

Because it's so annoying to type StringComparison.OrdinalIgnoreCase.

using StringThings.Extensions;
// all return true
"ABC".EqualsIgnoreCase("abc");
"ABC".StartsWithIgnoreCase("abc");
"ABC".EndsWithIgnoreCase("abc");
"ABC".ContainsIgnoreCase("abc");

Take First/Last Characters Extensions

No more length errors when using substring to get the leading or trailing characters from a string. Let this extension do the work for you of verifying that your substring doesn't exceed the length of the string you are evaluating.

Prevents this error: Index and length must refer to a location within the string.

using StringThings.Extensions;
"principal".TakeLast(3); // returns "pal"
"ABC-123".TakeFirst(3); // returns "ABC"
"tiny".TakeLast(5); // returns "tiny" (with no error!)
"This is a sentence".TakeFirst(8); //returns "This is "

IsNullOr* Extensions

Why use string.IsNullOrEmpty or string.IsNullOrWhiteSpace when it could be an extension method?

using StringThings.Extensions;
// all return true
((string)null).IsNullOrEmpty();
" ".IsNullOrWhiteSpace();

EqualsAny Extensions

Because writing new List<string> {"A", "B"}.Contains is annoying.

using StringThings.Extensions;
// all return true
var options =  new [] {"A", "B"};
"A".EqualsAny(options);
"B".EqualsAny(options);
"a".EqualsAnyIgnoreCase(options);

stringthings's People

Contributors

dlumpp avatar vincedio avatar

Stargazers

 avatar  avatar

Watchers

 avatar

stringthings's Issues

EqualsAny Extensions

The first "other useful extension" I have in mind is EqualsAny(this s, params string[] options) because writing new [] {"A", "B"}.Contains is annoying

Add Substring Extensions

Useful Substring operations could be

  • TakeFirst(int numChars)
  • TakeLast(int numChars)

Don't throw if they run out of chars to take

Thanks for the suggestion @VinceDio

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.