Beginner · Arrays, strings and null · topic 11 of 43 · 30 min
Strings: methods, interpolation, verbatim and raw strings
Explanation
What is a string?
A string is text: a sequence of char values. You can read its length and each character by index, just like an array.
string city = "Bengaluru";
Console.WriteLine(city.Length); // 9
Console.WriteLine(city[0]); // B
Console.WriteLine(city[^1]); // u
Console.WriteLine(city[0..4]); // BengImmutable: strings never change
Immutable means "cannot be changed after it is made". Every string method that seems to change text actually builds a new string and returns it. If you do not store the result, nothing changes. This is the most common string bug.
string name = " asha ";
name.Trim(); // result thrown away!
Console.WriteLine($"[{name}]"); // [ asha ]
name = name.Trim().ToUpper(); // store the new string
Console.WriteLine($"[{name}]"); // [ASHA]The most used methods
| Method | Example | Result |
|---|---|---|
| ToUpper / ToLower | "Pune".ToUpper() | "PUNE" |
| Trim | " hi ".Trim() | "hi" |
| Contains | "asha@okbank".Contains("@") | true |
| StartsWith / EndsWith | "INV-2041".StartsWith("INV") | true |
| IndexOf | "hello".IndexOf('l') | 2 (-1 if not found) |
| Substring(start, length) | "9876543210".Substring(0, 5) | "98765" |
| Replace | "10-05-2026".Replace("-", "/") | "10/05/2026" |
| Split | "rice,dal,oil".Split(',') | an array of 3 strings |
| string.Join | string.Join(" | ", items) | items joined by " | " |
| string.IsNullOrWhiteSpace | string.IsNullOrWhiteSpace(" ") | true |
Comparing strings
In C#, == compares the characters of two strings (unlike Java, where it compares references). But it is case-sensitive. For user input such as email addresses or coupon codes, compare without case.
string a = "Delhi", b = "delhi";
Console.WriteLine(a == b); // False
Console.WriteLine(string.Equals(a, b, StringComparison.OrdinalIgnoreCase)); // TrueInterpolation and formats
Put $ before the opening quote and write any expression inside { }. After a colon you can add a format, and after a comma an alignment width.
decimal price = 1499.5m;
DateTime d = new DateTime(2026, 8, 15);
Console.WriteLine($"Price: {price:F2}"); // Price: 1499.50
Console.WriteLine($"Date: {d:dd-MM-yyyy}"); // Date: 15-08-2026
Console.WriteLine($"[{"Tea",-6}][{20,5}]"); // [Tea ][ 20]Escape sequences and verbatim strings
string path1 = "C:\\Reports\\march.csv"; // \\ is one backslash
string path2 = @"C:\Reports\march.csv"; // verbatim: backslashes kept as typed
string quote = @"She said ""namaste"""; // in verbatim, "" is one quote
string twoLines = "Line 1\nLine 2"; // \n is a new lineRaw string literals (C# 11 and later)
A raw string starts and ends with three or more double quotes. Inside it, quotes and backslashes need no escaping, and it can span many lines. The spaces before the closing """ are removed from every line, so you can indent it with your code.
string name = "Asha";
string json = $$"""
{
"name": "{{name}}",
"city": "Pune"
}
""";
Console.WriteLine(json);Splitting and joining
string line = "Asha,21,Pune";
string[] parts = line.Split(',');
string name = parts[0];
int age = int.Parse(parts[1]);
Console.WriteLine($"{name} ({age}) from {parts[2]}");
string messy = "a,,b, c";
string[] clean = messy.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Console.WriteLine(string.Join("|", clean)); // a|b|cCommon mistakes and fixes
- Calling name.ToUpper(); without storing the result: strings are immutable. Write name = name.ToUpper();
- Substring past the end: "abc".Substring(1, 5) throws ArgumentOutOfRangeException. Check Length first.
- Comparing with == when case may differ: use StringComparison.OrdinalIgnoreCase.
- Building a Windows path with single backslashes in a normal string: "C:\new" would contain a new line. Use @"..." or Path.Combine.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Strings: methods, interpolation, verbatim and raw strings · 1/7
In this lesson you will work with text: strings.
Prefer a video by a teacher? Find free videos on “C# string methods interpolation verbatim raw string literals tutorial” on YouTube ↗
In real life: Masking a mobile number on a receipt
Shops and banks print receipts that hide most of the customer's mobile number, showing only the first two and last two digits. A few string methods do this, and also clean up what the cashier typed.
string typed = " 98765 43210 ";
string mobile = typed.Replace(" ", "").Trim();
if (mobile.Length != 10 || !mobile.All(char.IsDigit))
{
Console.WriteLine("Invalid mobile number");
return;
}
string masked = mobile[..2] + new string('X', 6) + mobile[^2..];
Console.WriteLine($"Receipt sent to {masked}");Practice (0/4 done)
string s = "Mumbai"; What is s.Substring(1, 3)?
What does this print? string s = "chai"; s.ToUpper(); Console.WriteLine(s);
What does "rice,dal,,oil".Split(',').Length give?
Write a method FormatName(string raw) that takes messy input like " rAVi kUMAR " and returns "Ravi Kumar": trim it, split on spaces (ignoring empty parts), make each word start with a capital and the rest small, and join with one space.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- StringBuilder and string performanceArrays, strings and null · same module
- null, nullable types and null-safe operatorsArrays, strings and null · same module
- Strings and template literalsJavaScript Programming (language in depth) · Strings, decisions and loops
- Strings, bytes and runesGo Programming (backend and cloud) · Collections and structs
- Strings in depthFrontend Web Development · JavaScript in depth
- Strings are char arraysC Programming (from zero to systems) · Strings in C
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does immutable mean for strings?
Answer: Once made, a string's characters cannot change; methods return new strings
Every "change" creates a new string.
Which string keeps C:\Temp\new.txt exactly as typed?
Answer: @"C:\Temp\new.txt" with single backslashes inside
A verbatim string (@) turns off escape sequences, so \n stays as backslash-n.
decimal p = 5m; What is $"{p:F2}"?
Answer: 5.00
F2 means fixed-point with two decimals.
In C#, what does "abc" == "abc" compare?
Answer: The characters (the text)
For strings, == compares the text.
"hello".IndexOf('z') returns...
Answer: -1
-1 means the character was not found.
What does 'a,b,,c'.split(',') give?Related
Answer: ['a', 'b', '', 'c']
Two commas in a row make an empty string between them.
What does strings.Fields(" a b ") return?Related
Answer: [a b]
Fields splits on runs of white space and drops empty parts.
Which gives the last character of the string text?Related
Answer: text.at(-1)
at(-1) counts from the end. text[-1] is undefined, there is no last() method, and slice(0, 1) gives the first character.
From Strings in depth
How many bytes does "UPI" occupy as a string literal?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Arrays, strings and null” before its exam: StringBuilder and string performance, null, nullable types and null-safe operators.
- Module 3 exam: Arrays, strings and null · 13 questions · 20 min, without help. 4 topics of this module still to finish first.
- Then the next module, Classes and objects. 43 topics left in the course before the final exam.