Quick win: join a full name
Apex basics · 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 would give AustinUSA. The separator is text too — ' ', '-' or ', '.
String city = 'Austin';
String country = 'USA';
String label = city + ', ' + country;
label is Austin, USA. For a full name, use a single space as the glue.
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 LovelacefullName('Grace', 'Hopper')→Grace Hopper
Requirements:
- Join
first, a space andlastwith+. - Exactly one space, and nothing at the start or end.
- 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.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.