Skip to content

Latest commit

 

History

History
77 lines (61 loc) · 2.54 KB

README.md

File metadata and controls

77 lines (61 loc) · 2.54 KB

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);