Strings#
// Simple concatenation
var firstName = "Ada";
var lastName = "Lovelace";
var fullName = firstName + " " + lastName; // "Ada Lovelace"
// Using a string like an array
char firstChar = firstName[0]; // 'A'
char secondLastChar = lastName[^2]; // 'c'
// 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"
// Multiline string with interpolation (Raw string literals)
var table = """
+---+---+---+---+
| x | | x | |
| y | | | |
+---+---+---+---+
""";
// A string has much more methods. Explore them by using code completion:
// Type `.` after the string to see all the available methods.
bool lastCharIsDot = fullName.EndsWith('.');
Activity 21 (Personal data check mail template)
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.
Employee[] employees =
[
new ("Lars", "Nielsen", "Østerbrogade 123, 4. th\n2100 København Ø", "+45 2012 3456"),
new ("Maria Isabel", "García", "Calle Mayor 45, 2ºB\n28013 Madrid", "+34 612 345 678"),
new ("Fulya", "Tayşi", "Ataköy 9. kısım, A-25B D.35, Bakırköy\n34156 İstanbul", "+90 212 559 67 40")
];
foreach (var e in employees) {
// YOUR CODE HERE
Console.WriteLine(e.Forenames + e.Surname + e.Address + e.Telephone);
}
internal record Employee(
string Forenames,
string Surname,
string Address,
string Telephone);