Strings#
// Simple concatenation
var firstName = "Ada";
var lastName = "Lovelace";
var fullName = firstName + " " + lastName; // "Ada Lovelace"
// String interpolation
var year = 1843;
var bio = $"Born in {year}, {fullName} is considered the first computer programmer.";
// "Born in 1843, Ada Lovelace is considered the first computer programmer."
// Using String.Format (older style, but useful for templates)
var persInfoTemplate = "{0}, {1} – {2}";
// a template can be used many times and for separating data and program logic
var formatted = string.Format(persInfoTemplate, lastName, firstName, year);
// "Lovelace, Ada – 1843"
// Joining a collection
List<string> fruits = ["apple", "banana", "cherry"];
var csv = string.Join(" | ", fruits); // "apple | banana | cherry"
// Multiline string with interpolation (Raw string literals)
var table = """
+---+---+---+---+
| x | | x | |
| y | | | |
+---+---+---+---+
""";
Activity 20
You want to send a yearly personal data check to all employees. The draft handed to you by your employee looks like this:
Dear _FORENAMES_ _SURNAME_, we yearly make sure that your contact data is valid. Please check if your address and telephone data are up to date:
Address
Telephone
If not valid, contact us on _EMAIL_.
The placeholders begin and end with an underscore _
. The Email address must be support-dk@fed.company
for Danish phone numbers and support@fed.company
for others.
Create a template string for the email above. Use the following template which prints all the data from an example dataset.
var employees = new[]
{
new Employee("Lars", "Nielsen", "Østerbrogade 123, 4. th\n2100 København Ø", "+45 2012 3456"),
new Employee("Maria Isabel", "García", "Calle Mayor 45, 2ºB\n28013 Madrid", "+34 612 345 678")
};
foreach (var e in employees) {
Console.WriteLine(e.Forenames + e.Surname + e.Address + e.Telephone);
// YOUR CODE HERE
}
internal record Employee(
string Forenames,
string Surname,
string Address,
string Telephone);