A string in C# is an immutable sequence of UTF-16 characters. Immutable is the word that explains most of the surprises: every method that looks like it changes a string actually returns a new one, and the original is untouched. That is why Replace and Trim have return values, and why building a string in a loop is slow enough to need StringBuilder.
Strings are the biggest topic on this blog. Almost every program reads text, checks it, reshapes it or writes it out. The articles below are grouped by the job you are doing, so something that lives on Regex sits beside something that lives on string.
New to C#, start with the first section. Otherwise the headings should get you to the right article in one click.
String Basics and Formatting
How to build a string, put values inside it and control how it looks.
- Standard and Custom Numeric Format Strings
- String Interpolation
- string vs FormattableString vs IFormattable
- The String Methods
- Make The First Letter of a String Upper Case
- How to Truncate a String
- Using Variables Inside Strings
- How to Convert String to Title Case
- How to Convert String from Title Case to camelCase
- Different Ways to Initialize a String
- How to Add Parameters to a String
Bytes, Encoding and Memory
A string is characters to you and bytes to everything else. These articles cover the crossing, and how to do it without allocating.
- Efficiently Converting Strings With UTF-8 String Literals
- Consistent Byte Representation of Strings in C# Without Encoding
- Converting String to Byte Array
- Converting a Byte Array to Hexadecimal String
- Converting a Hexadecimal String to a Byte Array
- How to Convert a String to a Span
- How to Use StringPool to Reduce String Allocations
Splitting, Joining and Rearranging
Cutting a string apart and putting it back together, which is most of the text work in a normal application.
- Permutations of a String
- Different Ways to Split a String
- The Fastest Way to Remove the Last Character of a String
- Replace Line Breaks in a String
- How to Create a Comma-Separated String From a List of Strings
- How to Escape the Backslash Character
- How To Remove HTML Tags From a String
- Fastest Way to Get the First N Characters of a String
- How to Reverse a Number as an Integer and Not as a String
- How to Reverse a String
Searching, Comparing and Checking
Asking a string a question: does it contain this, where is it, is it empty.
- How to Check if a String Contains Only Letters
- Check If a String Array Contains a Value and Get Index
- How to Check if StringBuilder Is Empty
- Count the Number of Vowels in a String
- How to Find All the Positions of a Substring in Another String
- How to Check if a String Ends With a Number
- Using Trie Class for Efficient Text Pattern Searching
Converting Between Strings and Other Types
Text in, value out, and back again. Most of these have a fast way and a safe way, and the articles show both.
- Convert String and Int to Enum
- How to Convert String to Bool
- How to Identify If a String is a Number
- How to Convert String to Char
- How to Convert Char Array to String
- How to Convert Int to String
- How to Convert String Array to String
- Fastest Way to Find and Extract a Number From a String
- How to Convert String to Int
Regular Expressions, Parsing and Text Sources
When the text has structure, you stop using string methods and start using a parser.
- How to Build a Query String for a URL
- Parsing HTML With AngleSharp
- Improve Performance With Source-Generated RegEx
- How to Append or Update Query String Values
- How to Read a String From a .resx (Resource) File
- How to Validate Email Address
- Encrypting and Decrypting a String
More String Techniques
Everything else we have written about strings and text.
- Base64 Encode and Decode
- Multidimensional Array vs Jagged Array
- ToString Method
- Introduction to Regular Expressions
- A Few Different Ways to Concatenate Strings
- StringBuilder
- Differences between String and string
- Delegates
- How to Perform Case-Insensitive Substring Search
- How to Get Class Name as String
- Counting Occurrences of a Char Within a String
- How to Remove All Whitespace Characters From a String
- How to Use HTML Agility Pack
- How to Get an Enum Member as a String
- How to Escape Curly Brackets and Special Characters
- Copy the Entire Contents of a Directory
Where to Go Next
Where text usually goes next:
Most string work is fast enough that readability wins. Reach for StringBuilder, Span and StringPool once you have measured a problem.
