Chapter 5: Strings and String Manipulation

1. What is a String in C#? (Super Simple Explanation)

A string is just a sequence of characters (letters, numbers, symbols, spaces…).

Examples:

  • “Hello”
  • “Webliance from Hyderabad”
  • “12345”
  • “user@example.com”
  • “” (empty string)

Important facts about strings in C#:

  • Strings are immutable → once created, you cannot change them! (Every time you “modify” a string, C# actually creates a new string in memory.)
  • Strings are reference types (like objects), not value types.
  • They are stored in a special area called the string intern pool (helps save memory).

2. Creating Strings (Different Ways)

C#

Verbatim strings (@) are super useful for file paths:

C#

3. String Interpolation – The Modern & Beautiful Way ($””)

Introduced in C# 6 – this is the recommended way in 2026!

C#

Advanced formatting inside interpolation:

C#

4. Most Important String Methods (You’ll Use These Every Day!)

Method What it does Example Result
Length Number of characters “Hello”.Length 5
ToUpper() / ToLower() Convert case “Webliance”.ToUpper() “WEBLIANCE”
Trim() / TrimStart() / TrimEnd() Remove whitespace ” Hello “.Trim() “Hello”
Substring(start, length) Extract part of string “Hello World”.Substring(6, 5) “World”
Replace(old, new) Replace all occurrences “I love C#”.Replace(“C#”, “C# in 2026”) “I love C# in 2026”
Split(separator) Split into array “a,b,c”.Split(‘,’) [“a”, “b”, “c”]
IndexOf(text) Find position (or -1 if not found) “Hello World”.IndexOf(“World”) 6
Contains(text) Check if contains “Hello”.Contains(“ell”) true
StartsWith() / EndsWith() Check beginning/end “file.txt”.EndsWith(“.txt”) true
IsNullOrEmpty() Check if null or empty (static method) string.IsNullOrEmpty(“”) true
IsNullOrWhiteSpace() Null, empty or only spaces string.IsNullOrWhiteSpace(” “) true

Real-life example – Cleaning user input:

C#

5. StringBuilder – For Performance When Building Large Strings

Because strings are immutable, doing this is very bad for performance:

C#

Solution: Use StringBuilder

C#

Handy StringBuilder methods:

C#

Mini-Project: Smart Name Formatter

C#

Your Homework (Fun & Practical!)

  1. Create a new console project called StringMaster
  2. Ask the user for:
    • Their full name
    • Their email
    • A short bio (2–3 sentences)
  3. Then print a nice report using StringBuilder that includes:
    • Name in uppercase
    • Email in lowercase
    • First 10 characters of bio
    • Whether email contains “@”
    • Number of words in bio
    • A formatted greeting message

Next lesson: Control Flow (if, else, switch) – we’re going to make programs that make smart decisions!

You’re doing incredibly well! 🎉 Any part of strings confusing you? Want more examples? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *