C# – Remove non-alphanumeric characters from a string

The simplest way to remove non-alphanumeric characters from a string is to use regex: Note: Don’t pass in a null, otherwise you’ll get an exception. Regex is the simplest options for removing characters by “category” (as opposed to removing arbitrary lists of characters or removing characters by position). The downside is that regex is the … Read more

C# – Trim a UTF-8 string to the specified number of bytes

Here’s the simplest way to efficiently trim a UTF-8 string to the specified number of bytes: A UTF-8 string can have a mix of characters between 1 to 4 bytes. When you only take part of the byte array, you may end up cutting multi-byte chars in half, which then get replaced with the replacement … Read more

C# – Check if a string contains any substring from a list

There are many different scenarios where you might want to check a string against a list of substrings. Perhaps you’re dealing with messy exception handling and have to compare the exception message against a list of known error messages to determine if the error is transient or not. When you need to check a string … Read more

C# – Get types from assembly (reflection-only load)

You can get all types from an assembly by doing a reflection-only load. This allows you to read type info from metadata without running the typical errors associated with fully loading an assembly. The way to do a reflection-only load is different in .NET Framework and .NET Core. I’ll show examples of how to do … Read more

HackerRank – Two Strings solution

In this article, I’ll explain how to solve the Two Strings algorithm problem on HackerRank. Problem statement: Given two strings, determine if they have a substring in common. The strings can have up to 100k characters. Example: Given “hello world” and “world”, do they have a substring in common? Yes, they many substrings in common. … Read more