Lesson 37 min read

Strings & Interpolation

Text is everywhere — learn to bend it to your will

Strings Are Everywhere

Almost every program deals with text — names, messages, passwords, URLs, emails. In C#, text lives inside string variables. A string is basically a chain of characters stored as a character array under the hood (that's literally why it's called a "string").

Here's the cool part: C# strings are immutable. That means once you create a string, you can't change it. Every time you "modify" a string, C# actually creates a brand new one behind the scenes. It's like writing in pen — you can't erase, you just write a new page.

String Basics & Interpolation

string name = "Luna";
int age = 10;
// String interpolation — the modern way (use this!)
Console.WriteLine($"Hi, I'm {name} and I'm {age} years old.");
// You can put ANY expression inside { }
Console.WriteLine($"Next year I'll be {age + 1}!");
Console.WriteLine($"My name has {name.Length} letters.");
Console.WriteLine($"Uppercase: {name.ToUpper()}");
// Multi-line with @ (verbatim string)
string address = @"123 Main Street
Apt 4B
New York, NY";
Console.WriteLine(address);
// Combine both: $@ for interpolated + multi-line
string card = $@"=================
Name: {name}
Age: {age}
=================";
Console.WriteLine(card);
Output
Hi, I'm Luna and I'm 10 years old.
Next year I'll be 11!
My name has 4 letters.
Uppercase: LUNA
123 Main Street
Apt 4B
New York, NY
=================
  Name: Luna
  Age:  10
=================

String Methods — Your Toolbox

Strings come packed with useful methods. Think of each method as a different tool:

  • .Length — how many characters (it's a property, no parentheses)
  • .ToUpper() / .ToLower() — shout or whisper
  • .Trim() — strips whitespace from both ends (great for user input)
  • .Contains("text") — does the string include this?
  • .StartsWith() / .EndsWith() — check the beginning or end
  • .Substring(start, length) — grab a piece of the string
  • .Replace(old, new) — swap text
  • .Split(separator) — break a string into an array
  • .IndexOf("text") — find where something appears (-1 if not found)

Remember: these methods don't change the original string. They return a new string. You need to save the result!

String Methods in Action

string sentence = " The quick brown fox jumps! ";
// Trim whitespace
string clean = sentence.Trim();
Console.WriteLine($"'{clean}'");
// Search
Console.WriteLine($"Contains 'fox': {clean.Contains("fox")}");
Console.WriteLine($"Starts with 'The': {clean.StartsWith("The")}");
Console.WriteLine($"Index of 'brown': {clean.IndexOf("brown")}");
// Extract
string word = clean.Substring(10, 5); // start at 10, take 5 chars
Console.WriteLine($"Extracted: {word}");
// Replace
string newSentence = clean.Replace("fox", "cat");
Console.WriteLine(newSentence);
// Split into words
string csv = "apple,banana,cherry,date";
string[] fruits = csv.Split(',');
foreach (string fruit in fruits)
{
Console.WriteLine($" Fruit: {fruit}");
}
Output
'The quick brown fox jumps!'
Contains 'fox': True
Starts with 'The': True
Index of 'brown': 10
Extracted: brown
The quick brown cat jumps!
  Fruit: apple
  Fruit: banana
  Fruit: cherry
  Fruit: date

StringBuilder — When Speed Matters

Remember how strings are immutable? That means every time you do result += "something" in a loop, C# creates a whole new string and throws the old one away. Do that 10,000 times and your program slows to a crawl.

StringBuilder is like a whiteboard — you can keep writing and erasing on the same board without making a new one each time. Use it when you're building up a big string piece by piece.

StringBuilder for Performance

using System.Text;
// Bad way (fine for small stuff, slow for loops)
string slow = "";
for (int i = 0; i < 5; i++)
slow += $"Item {i}, ";
Console.WriteLine(slow);
// Good way — StringBuilder
var sb = new StringBuilder();
for (int i = 0; i < 5; i++)
{
sb.Append($"Item {i}");
if (i < 4) sb.Append(", ");
}
Console.WriteLine(sb.ToString());
// StringBuilder tricks
var builder = new StringBuilder();
builder.AppendLine("Line 1");
builder.AppendLine("Line 2");
builder.Insert(0, "Title\n");
Console.WriteLine(builder.ToString());
Output
Item 0, Item 1, Item 2, Item 3, Item 4, 
Item 0, Item 1, Item 2, Item 3, Item 4
Title
Line 1
Line 2
Note: 🧠 Rule of thumb: If you're gluing strings together inside a loop (more than ~10 iterations), switch to StringBuilder. Outside of loops, regular string interpolation with $ is perfectly fine and way more readable.

Quick check

What does string interpolation ($"...") let you do?

Continue reading

Static ArraysData Structure
A row of numbered boxes — fast to grab, fixed in size
Trie (Prefix Tree)Data Structure
Insert, search, autocomplete
Operators & ExpressionsConditionals