Hergine Richemont

Learning just for the fun of it

string manipulation

C# Track, Exercism #45 High School Sweethearts

Relevant link


Github solution: exercism-solutions/high-school-sweethearts

Exercism #45: High School Sweethearts

Learning Goal

String Formatting | Verbatim Strings

Relevant Information

String formatting in C# is just filling in blanks in a text template. Instead of gluing strings together with + signs, C# gives you two clean ways to insert variables into text:

1. The Classic Way: string.Format() (Composite Formatting)

Think of this like a numbered “fill-in-the-blank” sheet.

  • You use numbered placeholders like {0}, {1}, {2} inside your text.
  • You list your variables at the end in matching order.

Analogy: “Dear {0}, your order {1} is ready.” -> slot 0 gets the name, slot 1 gets the order number.

2. The Modern Way: $"" (String Interpolation)

This is the modern, readable way preferred by C# developers today.

  • Put a $ sign right before the opening quote.
  • Drop your variables directly inside {} right where you want them to appear.

Analogy: “Dear {name}, your order {orderNumber} is ready.”


Format Items

Format items don’t just insert your variables, they give you precise control over how those variables look and align on the screen.

Think of the full syntax as a 3-part blueprint:

The 3 Parts Breakdown

1. Value (Required)

The variable or index you want to print (e.g., {name} or {0}).

2. Alignment (Optional — starts with a comma ,)

Controls column width and padding with spaces:

  • Positive number (,10): Right-aligns the text inside a 10-character wide space.
  • Negative number (,-10): Left-aligns the text inside a 10-character wide space.

Great for making clean, perfectly aligned tables in text.

3. Format String (Optional — starts with a colon :)

Tells C# how to display numbers, dates, and times:

  • :d → Short date (02/25/2010)
  • :C → Currency ($55.50)
  • :N2 → Number with commas and 2 decimals (1,234.56)
  • :P → Percentage (85%)
  • :E → Scientific / Exponential notation (5.55E+001)

Verbatim strings

Verbatim strings allow multi-line strings. They are introduced with an @

Task 1: Display the couple’s name separated by a heart

Implement the static HighSchoolSweethearts.DisplaySingleLine() method to take two names and display them separated by a heart. The formatted text should be 61 characters wide, with the heart in the center of the string.

All names are guaranteed to fit well within the width of the line.

I used the Modern Way: $"" (String Interpolation) and the optional alignment feature using comma (,) with an alignment of 29 on the left and on the right leaving 3 characters space for the heart and the spaces between the heart and each name.

Task 2: Display the couple’s initials in an ascii art heart

Implement the static HighSchoolSweethearts.DisplayBanner() method which displays the two sets of initials separated with a plus sign.

I created 1 expression-bodied method to get the initials and used verbating”@” to design the art pattern using sting interpolation with alignment

Task 3: German exchange students should be made to feel at home with locale-sensitive declarations.

Implement the static HighSchoolSweethearts.DisplayGermanExchangeStudents() method to show date of start of relationship and length of time for our german exchange students.

I created a CultureInfo instance to get German culture formatting rules. The I applied the rules to the date and hours before returning the string


Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted