Start learning free

LESSON 69 OF 289 · STRINGS - CHALLENGES

Creating IDs in C#: Substring from the Start and the End

Practice Read the full explanation

A website creates a user ID from each person's name: the first 2 letters of the first name, followed by the last 3 letters of the last name.

Given the first name first and the last name last, return the ID.

 

Example 1:

Input: first = "John", last = "Smith"
Output: "Joith"
Explanation: "Jo" are the first 2 letters of "John" and "ith" are the last 3 letters of "Smith".

Example 2:

Input: first = "Jo", last = "Doe"
Output: "JoDoe"
Explanation: "Jo" has exactly 2 letters and "Doe" exactly 3, so both names are used whole.

Example 3:

Input: first = "ada", last = "Lovelace"
Output: "adace"
Explanation: The letters keep their case: the ID doesn't change any letter to upper or lower case.

 

Constraints:

  • first has at least 2 letters and last has at least 3 letters.
  • Both names are made of English letters only, lowercase or uppercase.
STEP 1 OF 1
    Ctrl + Enter to check

    C# · STRINGS - CHALLENGES

    Creating Ids in C#, explained

    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":

    PieceExpressionValue
    first partfirst.Substring(0, 2)"Jo"
    start of second partlast.Length - 32
    second partlast.Substring(2)"ith"
    IDthe 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. Using Length - 2 would keep only 2 letters.
    • Names of exactly the minimum length. "Jo" and "Doe" work: Substring(0, 2) takes the whole first name and Substring(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 Substring throw an ArgumentOutOfRangeException.

    Testing

    firstlastResult
    "John""Smith""Joith"
    "Jo""Doe""JoDoe"
    "ada""Lovelace""adace"
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free