C# – Remove spaces from a string

The simplest way to remove spaces from a string is by using string.Replace(). Here’s an example: This outputs the following. Notice the spaces are removed. string.Replace() is good for removing all occurrences of a specific character (” ” in this case). When you want to remove all whitespace characters (spaces, tabs, newlines, etc..), there are … Read more

CA1806: Do not ignore method results

The CA1806 warning is triggered whenever you call a method and ignore the return value. I’ll show examples of a few of the scenarios where you might run into this. CA1806 – When you call a string method and don’t use the new string Strings are immutable. Once you create a string, you can’t change … Read more

C# – Remove a list of characters from a string

When you want to remove a list of characters from a string, loop through the list and use string.Replace(): Note that string.Replace() returns a new string (because strings are immutable). Running this outputs the following: This is the fastest approach (in .NET 6+). Linq approach: Where() + ToArray() + new string() Another option for removing … Read more

C# – Get a file’s checksum using any hashing algorithm

This article shows how to get a file’s checksum using any of these hashing algorithms: MD5, SHA1, SHA256, SHA384, and SHA512. If you are only interested getting a specific type of checksum, take a look at the first section. If you’re interested in a general-purpose checksum method that allows you to generate the checksum using … Read more

PowerShell – Get a file’s MD5 checksum

To get a file’s MD5 checksum, you can use Get-FileHash. This has been available since PowerShell v4. Use it like this: This file’s MD5 checksum is AE34D271ACC9C242BC9EED2E0EA72093. If Get-FileHash isn’t available Get-FileHash was put in PowerShell v4. If you’re on an earlier version, you’ll get following error when you try to use this: Get-FileHash is … Read more

C# – Hex string to byte array

This article shows code for converting a hex string to a byte array, unit tests, and a speed comparison. First, this diagram shows the algorithm for converting a hex string to a byte array. To convert a hex string to a byte array, you need to loop through the hex string and convert two characters … Read more