C# & .NET Framework Arrays & String Mastery
💡
Exercise 28

String Methods — The Label Maker 15 XP Medium

Ctrl+Enter Run Ctrl+S Save

🏷️ The Architect's Label Maker

On every construction site, labels are everywhere — door signs, blueprint tags, safety notices, material codes. The architect needs a reliable label maker, and in .NET, the string class is exactly that: a rich, powerful toolkit for manipulating text.

In C#, string is an alias for System.String. Strings are immutable — every operation that appears to modify a string actually creates a new string object on the heap. This immutability makes strings thread-safe and enables an optimization called string interning, but it also means careless string manipulation can generate lots of garbage for the GC to collect.

🔤 Case Conversion

Converting text to uppercase or lowercase is a daily task for the label maker — project names go on official signage in uppercase, while internal logs use lowercase.

string project = "Skyline Tower"; Console.WriteLine(project.ToUpper()); // SKYLINE TOWER Console.WriteLine(project.ToLower()); // skyline tower // Culture-aware versions for international projects Console.WriteLine(project.ToUpperInvariant()); // SKYLINE TOWER Console.WriteLine(project.ToLowerInvariant()); // skyline tower

✂️ Substring and Indexing

Substring extracts a portion of the string. IndexOf finds where a character or substring first appears. Together, they're like a precision cutting tool for labels.

string code = "PROJ-SKY-2024-TOWER"; // Substring(startIndex) — from index to end Console.WriteLine(code.Substring(5)); // SKY-2024-TOWER // Substring(startIndex, length) — extract exact portion Console.WriteLine(code.Substring(5, 3)); // SKY // IndexOf — find position Console.WriteLine(code.IndexOf("2024")); // 9 Console.WriteLine(code.IndexOf('T')); // 14 // LastIndexOf — find last occurrence Console.WriteLine(code.LastIndexOf('-')); // 13

🔄 Replace, Trim, and Contains

These methods handle cleanup and verification — essential when processing messy input from site reports.

string input = " Steel, Wood, Glass "; // Trim — remove leading/trailing whitespace Console.WriteLine(input.Trim()); // "Steel, Wood, Glass" Console.WriteLine(input.TrimStart()); // "Steel, Wood, Glass " Console.WriteLine(input.TrimEnd()); // " Steel, Wood, Glass" // Replace — swap characters or substrings string updated = input.Trim().Replace("Wood", "Titanium"); Console.WriteLine(updated); // "Steel, Titanium, Glass" // Contains, StartsWith, EndsWith — boolean checks Console.WriteLine("Skyline Tower".Contains("Tower")); // True Console.WriteLine("Skyline Tower".StartsWith("Sky")); // True Console.WriteLine("Skyline Tower".EndsWith("er")); // True

🔪 Split and Join — Disassemble and Reassemble

Split breaks a string into an array of substrings. Join fuses an array back into a single string. They're the architect's ability to take apart a label and put it back together in a new format.

string csv = "Steel,Wood,Glass,Concrete"; // Split on comma string[] parts = csv.Split(','); Console.WriteLine(parts.Length); // 4 Console.WriteLine(parts[2]); // Glass // Join with a different separator string spaced = String.Join(" | ", parts); Console.WriteLine(spaced); // Steel | Wood | Glass | Concrete // Split with multiple delimiters string messy = "Steel;Wood,Glass|Concrete"; string[] items = messy.Split(new char[] { ';', ',', '|' }); Console.WriteLine(items.Length); // 4

📝 String Interpolation and Formatting

Modern C# provides elegant ways to compose strings from variables and expressions.

string name = "Skyline Tower"; int year = 2024; // String interpolation (C# 6+) — the preferred way string label = $"Project: {name} ({year})"; Console.WriteLine(label); // Project: Skyline Tower (2024) // Composite formatting with String.Format string formatted = String.Format("Code: {0}-{1}", "SKY", year); Console.WriteLine(formatted); // Code: SKY-2024 // Padding — great for aligned output Console.WriteLine("Item".PadRight(15) + "Qty"); Console.WriteLine("Steel Beams".PadRight(15) + "42"); Console.WriteLine("Glass Panels".PadRight(15) + "88");

🔃 Reversing a String

C# doesn't have a built-in string.Reverse(), but you can convert to a char array, reverse it, and create a new string:

string name = "Skyline Tower"; char[] chars = name.ToCharArray(); Array.Reverse(chars); string reversed = new string(chars); Console.WriteLine(reversed); // rewoT enilykS

⚡ Key Takeaways

  • Strings are immutable — every modification creates a new string object.
  • ToUpper() / ToLower() for case conversion; use Invariant versions for culture-independent comparisons.
  • Substring(), IndexOf(), and LastIndexOf() for extracting and locating parts of a string.
  • Split() turns a string into an array; String.Join() turns an array back into a string.
  • Use $"..." string interpolation for readable string composition.
  • PadLeft() / PadRight() for aligned, fixed-width output.
  • Reverse a string by converting to char[], calling Array.Reverse(), then new string(chars).
📋 Instructions
## 🏷️ Architect's Label Maker Challenge The architect needs a set of labels for project **Skyline Tower**. Build the label maker! 1. Create a `string` variable `project` with value `"Skyline Tower"` 2. Print the project in UPPERCASE: `"PROJECT: SKYLINE TOWER"` 3. Print the project in lowercase: `"project: skyline tower"` 4. Create a project code by taking the first 3 characters of `project`, converting to uppercase, and combining with `"-2024"`. Print: `"Code: SKY-2024"` 5. Split `project` by `' '` (space) and print the word count: `"Words: 2"` 6. Reverse the string `project` and print: `"Reversed: rewoT enilykS"`
Use `project.ToUpper()` and `project.ToLower()` for case conversion. For the code, use `project.Substring(0, 3).ToUpper() + "-2024"`. Split with `project.Split(' ')` and check `.Length`. To reverse, convert to `char[]` with `ToCharArray()`, call `Array.Reverse()`, then `new string(chars)`.
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.