ApexSensei

Drills › Write Apex

Quick win: join a full name

Write Apex · Beginner · 5 min · Runs in your Salesforce Developer org · Free

Scenario. A list shows each Contact's full name on one line.

Put a first and last name together with one space between them.

What you will learn

When you join two variables you must add the glue yourself. city + country gives AustinUSA. The separator is text too — ', ' here.

String cityCountry(String city, String country) {
    return city + ', ' + country;
}

The task

Write a method fullName(String first, String last) that returns the two names with one space between them.

Examples:

  • fullName('Ada', 'Lovelace') → Ada Lovelace
  • fullName('Grace', 'Hopper') → Grace Hopper

Requirements:

  1. Join first, a space and last with +.
  2. Exactly one space, and nothing at the start or end.
  3. Keep the method's name and its two String inputs.

Starter code

String fullName(String first, String last) {
    return null;
}

System.debug(fullName('Ada', 'Lovelace'));
System.debug(fullName('Grace', 'Hopper'));

When it passes

`fullName` returns `<first> <last>` for every pair of names the grader tries.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Quick win: shout it · Quick win: is the name blank? ›