Problem overview
The method CreatingIds receives a first name and a last name and returns an ID: the first 2 letters of first, followed by the last 3 letters of last. For "John" and "Smith" the ID is "Joith". For "Jo" and "Doe" it is "JoDoe", because both names are used whole. For "ada" and "Lovelace" it is "adace": the letters keep their case.
Approach: one piece from each end
The ID has two parts, and each is a slice of a string. The first part starts at index 0 and has a known length, 2. The second part ends where last ends and has a known length, 3, so it starts 3 characters before the end: at index last.Length - 3.
The two forms of Substring
For the first part, use Substring(start, length). Its second argument is how many characters to take, not the position to stop at. "John".Substring(0, 2) starts at 0 and takes 2 characters: "Jo".
For the second part, Substring(start) goes from start to the end. With start equal to last.Length - 3, it returns exactly the last three letters. For "Smith" (length 5) the start is 2, and "Smith".Substring(2) is "ith".
The solution in C#
class Solution {
public string CreatingIds(string first, string last) {
return first.Substring(0, 2) + last.Substring(last.Length - 3);
}
}
Both calls return new strings (strings are immutable), and + joins them. No case conversion happens, so the result keeps the letters as written.
Step by step
The lesson is one step. Writing it as a short table of the pieces helps to see where each number comes from, using "John" and "Smith":
| Piece | Expression | Value |
|---|---|---|
| first part | first.Substring(0, 2) | "Jo" |
| start of second part | last.Length - 3 | 2 |
| second part | last.Substring(2) | "ith" |
| ID | the two parts joined | "Joith" |
The name of the second part's start is computed from the length instead of written as a fixed number, because last names have different lengths. "Doe" has length 3, so its start is 0 and the whole name is used.
Other ways to write it
With ranges, available since C# 8, the same ID can be written without any arithmetic: first[..2] means "up to index 2, not included" and last[^3..] means "from the third character from the end to the end".
string id = first[..2] + last[^3..];
For "John" and "Smith" this gives "Joith" too. The lesson's version with Substring teaches the idea underneath: a start position and, optionally, a length. Notice the difference in the meaning of numbers: in Substring(0, 2) the 2 is a length, while in first[..2] it is an end position.
Edge cases and common mistakes
- Treating the second argument as an end index.
first.Substring(0, 2)means "2 characters from index 0". Some languages take an end position instead; in C# it is a length. - Off-by-one at the end. The last 3 letters start at
Length - 3. UsingLength - 2would keep only 2 letters. - Names of exactly the minimum length.
"Jo"and"Doe"work:Substring(0, 2)takes the whole first name andSubstring(0)takes the whole last name. - Names that are too short. The lesson promises at least 2 and 3 letters. Without that, a one-letter first name or a two-letter last name makes
Substringthrow anArgumentOutOfRangeException.
Testing
| first | last | Result |
|---|---|---|
"John" | "Smith" | "Joith" |
"Jo" | "Doe" | "JoDoe" |
"ada" | "Lovelace" | "adace" |
