How To

How to Trim a UTF-8 string to the specified number of bytes in C#
By Tan Lee Published on Feb 05, 2025 419

To trim a UTF-8 string to a specified number of bytes in C#, you need to account for the fact that characters in UTF-8 encoding can vary in size, ranging from 1 to 4 bytes.

How to Save a list of strings to a file in C#
By Tan Lee Published on Feb 05, 2025 431

The simplest way to save a list of integers to a file is by using File.WriteAllLines().

How to Convert string list to float list in C#
By Tan Lee Published on Feb 05, 2025 445

You can convert a list of strings to a list of floating-point numbers using float.Parse() on all the strings.

How to Remove a list of characters from a string in C#
By Tan Lee Published on Feb 05, 2025 457

When you want to remove a list of characters from a string, you can loop through the list and use string.Replace():

How to Check if a string contains any substring from a list in C#
By Tan Lee Published on Feb 05, 2025 1.26K

There are various use cases where you may need to verify whether a string contains any element from a list of substrings.

Find a character in a string in C#
By Tan Lee Published on Feb 05, 2025 342

There are several ways to find a character in a string in C#:

Remove non-alphanumeric characters from a string in C#
By Tan Lee Published on Feb 05, 2025 704

A simple approach to removing non-alphanumeric characters from a string is by utilizing a regular expression (regex):

Ignore case with string.Contains() in C#
By Tan Lee Published on Feb 05, 2025 314

By default, string.Contains() performs a case-sensitive search for a substring (or character) within a string.

Deserialize JSON using different property names in C#
By Tan Lee Published on Feb 05, 2025 1.05K

When working with JSON data where property names don’t match your class property names, and you cannot alter the JSON or class structure, you have three main options to handle this discrepancy:

Deserialize JSON to a derived type in C#
By Tan Lee Published on Feb 05, 2025 788

To deserialize JSON into a derived type, you typically embed the type name in the JSON string. During deserialization, the system checks the type name against known derived types and deserializes it to the correct type.

Deserialize JSON to a dictionary in C#
By Tan Lee Published on Feb 05, 2025 1K

To deserialize JSON into a dictionary in C#, you can use the built-in JsonSerializer.Deserialize() method from the System.Text.Json namespace or use JsonConvert.DeserializeObject() from the popular Newtonsoft.Json library.

Deserialize a JSON array to a list in C#
By Tan Lee Published on Feb 05, 2025 892

To deserialize a JSON array into a list in C#, you can use the JsonSerializer class from the System.Text.Json namespace.