Skip to content

Repository files navigation

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 stringpublicclassDbConnectionString:TypedString{publicDbConnectionString(stringvalue):base(value){}}// register instance with dependency injectionservices.AddSingleton(newDbConnectionString("...from config..."));// ask for the registered DbConnectionString in your apppublicclassHomeController(DbConnectionStringconnStr){publicActionResultIndex(){varconn=newSqlConnection(connStr);}}

Case-insensitive Comparison Extensions

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

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

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

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

EqualsAny Extensions

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

usingStringThings.Extensions;// all return truevaroptions=new[]{"A","B"};"A".EqualsAny(options);"B".EqualsAny(options);"a".EqualsAnyIgnoreCase(options);

About

Handy string extensions and utilities for .NET

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages