C# – Ignore case with string.Contains()

By default, string.Contains() does a case sensitive search for a substring (or character) in a string. You can make it do a case insensitive search instead by passing in StringComparison.OrdinalIgnoreCase, like this: Note: StringComparison has three ‘ignore case’ options to choose from. Because this is doing a case insensitive search, it matched “earth” to “Earth” … Read more

C# – Find a character in a string

There are three methods you can use to find a character in a string: I’ll show examples below. Using string.IndexOf() to find a character string.IndexOf() returns the index of the first occurrence of a character in a string. It searches the string from left to right, starting at the beginning. If it doesn’t find the … Read more

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

C# – Remove first or last character from a string

Use string.Remove() to remove character(s) from a string based on their index, such as the first or last character. This method has two parameters: string.Remove() returns a new string with the characters removed. I’ll show examples below. Remove the first character from a string To remove the first character, use string.Remove(startIndex: 0, count: 1), like … Read more

C# – How to convert string to int

There are three ways to convert a string to an int: I’ll show examples of using these approaches. Use int.Parse() int.Parse() takes a string and converts it to a 32-bit integer. Here’s an example of using int.Parse(): When conversion fails int.Parse() throws an exception if it can’t convert the string to an int. There are … Read more

C# – Using String.Join()

You can use String.Join() to convert a collection of items to a string with a separator (such as a comma). Here’s an example of using String.Join() to convert a List of strings to a comma-separated string: This results in the following comma-separated string: String.Join() can be used on wide variety of input: I’ll show a … Read more

C# – Convert a List to a string

There are two good ways to convert a List<T> to a string: I’ll show examples of both approaches. Using String.Join() String.Join() is the simplest way to convert a List to a string. With this method, you pass in the List and a delimiter and it outputs a string. You can use any delimiter you want. … Read more

C# – Parse a comma-separated string into a list of integers

Let’s say you want to parse a comma-separated string into a list of integers. For example, you have “1,2,3” and you want to parse it into [1,2,3]. This is different from parsing CSV with rows of comma-separated values. This is more straightforward. You can use string.Split(“,”) to get the individual values and then convert them … 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# – How to use format strings with string interpolation

Interpolated strings have the following structure: {variable:format}. Typically you exclude the format, so they normally look like this: $”My name is {name}”. Here’s how to use format strings with an interpolated string: This outputs the following: This is the equivalent of using string.Format() like this: As you can see, specifying a format string with an … Read more

C# – Use string interpolation instead of string.Format

Using string.Format() is error prone and can lead to runtime exceptions. Using string interpolation prevents these problems (and it’s more readable). This article shows the two common mistakes you can make when using string.Format(), resulting in runtime exceptions, and shows how you can use string interpolation to prevent these problems. 1 – FormatException: Format string … 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

C# – Use Convert.ChangeType to convert string to any type

You can use Convert.ChangeType() to convert from a string to any type, like this: Normally you’d call the specific type converter method, like Convert.ToBoolean(“true”). However, sometimes it makes sense to use the generalized type converter method – Convert.ChangeType() – instead of hardcoding the calls to specific type converter methods. I’ll show an example of when … Read more