C# & .NET Framework Variables & Data Types
💡
Exercise 8

Strings & Characters — The Architect's Engraving Tools 15 XP Medium

Ctrl+Enter Run Ctrl+S Save

✍️ Engraving Words in Stone

Every building the .NET Architect designs has inscriptions — names etched above doorways, plaques on walls, signs directing visitors. In C#, text is handled by the string type, and individual letters by the char type. Together, they are the Architect's engraving tools.

A string is an immutable sequence of characters. That word — immutable — is crucial. When you "change" a string, C# doesn't modify the original; it creates an entirely new one. Think of it like carving in marble: once it's etched, you can't erase it — you carve a new slab instead.

🔤 Creating Strings

// Regular string string greeting = "Hello World"; // String interpolation (the modern way — prefix with $) string name = "Architect"; string message = $"Welcome, {name}!"; // Verbatim string (prefix with @ — ignores escape sequences) string path = @"C:\Users\Architect\Plans"; // Escape sequences in normal strings string newLine = "Line1\nLine2"; // \n = new line string tab = "Col1\tCol2"; // \t = tab

🛠️ Essential String Operations

The string class comes loaded with methods. Here are the ones you'll use daily:

string text = "Hello World"; Console.WriteLine(text.ToUpper()); // HELLO WORLD Console.WriteLine(text.ToLower()); // hello world Console.WriteLine(text.Length); // 11 Console.WriteLine(text.Contains("World")); // True Console.WriteLine(text.Substring(0, 3)); // Hel Console.WriteLine(text.Replace("World", "C#")); // Hello C# Console.WriteLine(text.IndexOf("World")); // 6 Console.WriteLine(text.Trim()); // trims whitespace

✂️ Split & Join

Need to break a sentence into words, or glue words back together?

string csv = "brick,cement,steel"; string[] materials = csv.Split(','); // materials[0] = "brick", materials[1] = "cement", materials[2] = "steel" string joined = string.Join(" | ", materials); // "brick | cement | steel"

🔡 The char Type

A char holds exactly one Unicode character, enclosed in single quotes:

char letter = 'A'; char digit = '7'; char symbol = '#'; // Useful checks Console.WriteLine(char.IsLetter(letter)); // True Console.WriteLine(char.IsDigit(digit)); // True Console.WriteLine(char.IsUpper(letter)); // True // Access characters in a string by index string word = "Hello"; char first = word[0]; // 'H'

💡 Pro Tips

• Use string.IsNullOrEmpty(s) to check if a string is null or "".
• Use string.IsNullOrWhiteSpace(s) to also catch strings with only spaces.
• For heavy string manipulation in loops, use StringBuilder instead — it's mutable and much faster.

📋 Instructions
### Your Mission The Architect needs to test the engraving tools before carving the city's welcome sign. Perform these string operations on the text `"Hello World"`: 1. Print the string in **ALL UPPERCASE**. 2. Print the string in **all lowercase**. 3. Print the **length** of the string (just the number). 4. Print whether the string **contains** `"World"` — should print `True`. 5. Print the first **3 characters** using `Substring`. 6. Print the string with `"World"` **replaced** by `"C#"`. Expected output: ``` HELLO WORLD hello world 11 True Hel Hello C# ```
Use text.ToUpper(), text.ToLower(), text.Length, text.Contains("World"), text.Substring(0, 3), and text.Replace("World", "C#"). Print each result with Console.WriteLine().
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.