Quick win: make initials
Write Apex · Beginner · 5 min · Runs in your Salesforce Developer org · Free
Scenario. An avatar shows two letters when a Contact has no photo.
Take the first letter of two names, make them capitals and join them.
What you will learn
Text has built-in methods. left(1) gives the first character and toUpperCase() makes it a capital. You can chain them.
String first = 'jane';
String initial = first.left(1).toUpperCase();
initial is J. Do it for both names and join them with +.
The task
Write a method initials(String first, String last) that returns both initials as capitals, with nothing between them.
Examples:
initials('jane', 'doe')→JDinitials('Ana', 'Silva')→AS
Requirements:
- Take the first character of each name with
left(1). - Make each one a capital with
toUpperCase(). - Join the two with
+and return the result.
Starter code
String initials(String first, String last) {
return null;
}
System.debug(initials('jane', 'doe'));
System.debug(initials('Ana', 'Silva'));
When it passes
`initials` returns the two capital initials 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.
‹ Quick win: is it positive? · Quick win: pick the larger number ›