Limited Offer
60% OFF on all plans!

Strings in C#

Work with strings using string literals, verbatim strings (@""), raw string literals (C# 11+), string interpolation ($""), StringBuilder, and common String methods.

Creating Strings

1. Single Line Strings

Use double quotes to create a string. Escape sequences like `\n`, `\t`, `\\` work inside regular strings.

2. Verbatim Strings (@"")

Prefix with `@` to write strings without escape sequences — backslashes are treated literally. Ideal for file paths and multiline text.

3. Raw String Literals (C# 11+)

Raw string literals use `"""..."""` — no escaping needed at all. Great for JSON, HTML, regex, or any content with special characters.

4. @"..." vs """...""" — Key Difference

Both avoid escape sequences, but they serve different purposes. `@""` is verbatim — good for paths. `"""..."""` is raw — good for structured content like JSON, SQL, HTML.

5. String Interpolation ($"")

Use `$"..."` to embed expressions directly inside a string. Combine with `@` for verbatim interpolated strings.

String Basics (Indexing, Length & Traversal)

1. Accessing a Character at a Specific Index

Use `[index]` to get the character at a position. Strings are zero-indexed.

2. Getting String Length

`Length` property returns the number of characters in the string.

3. Traversing a String

Loop through each character using a `foreach` loop or a `for` loop with index.

Common String Methods

1. Searching & Checking

Check for substrings, prefixes, suffixes, and find positions.

2. Transforming Strings

Convert case, trim whitespace, replace, and pad strings.

3. Split and Join

Split a string into parts and join an array of strings back into one.

4. Substring & Slicing

Extract a portion of a string using `Substring()`, or use C# 8+ range syntax `[start..end]`.

Comparing & Concatenating Strings

1. Comparing Strings (Exact Match)

Use `==` for value equality. Use `string.Equals()` for case-insensitive comparison.

2. Lexicographic Comparison (Dictionary Order)

`string.Compare()` returns negative, zero, or positive — useful for sorting.

3. Concatenating Strings

Concatenate with `+`, `string.Concat()`, or interpolation. Use `StringBuilder` for many concatenations in a loop.

StringBuilder (Mutable Strings)

1. StringBuilder Basics

`StringBuilder` is mutable — it modifies in place instead of creating new strings. Use it when building strings with many concatenations.

2. StringBuilder in a Loop

Using `+` to concatenate in a loop is inefficient. `StringBuilder` is the right tool.

3. StringBuilder Method Chaining

`StringBuilder` methods return `this` — allowing fluent method chaining.

String Transformations

1. Reversing a String

Reverse a string using `char` array conversion or LINQ.

2. Uppercase, Lowercase & Repeat

Change case with `ToUpper()`/`ToLower()` and repeat a string with `string.Concat` or LINQ.

ASCII Conversions

1. Character to ASCII Code

Cast a `char` to `int` to get its ASCII/Unicode code point.

2. ASCII Code to Character

Cast an `int` to `char` to get the character for that code point.

Searching & Replacing

1. Replace

`Replace()` replaces all occurrences of a substring. Use regex for pattern-based replacement.

2. Trimming Strings

Remove unwanted whitespace or specific characters from the start, end, or both sides.

3. Null & Empty Checks

Use `string.IsNullOrEmpty()` and `string.IsNullOrWhiteSpace()` to safely check for empty strings.