Clean a Contact name
Write Apex · Beginner · 12 min · Runs in your Salesforce Developer org · Free
Scenario. A CSV import padded first names with spaces, used random capital letters, and added a temporary suffix to last names.
Trim spaces, fix capital letters, and drop a temporary suffix from imported Contact names.
What you will learn
trim() removes end spaces. toLowerCase() makes letters small, capitalize() makes the first a capital. Drop an unwanted suffix with removeEnd(text) (end only), remove(text) (every copy) or replace(text, '').
String clean = raw.trim().toLowerCase().capitalize();
'Chen__temp'.removeEnd('__temp'); // 'Chen'
Gotcha. capitalize() changes only the first letter. 'MAYA'.capitalize() is still MAYA.
The task
Clean two imported Contact names: trim the spaces, fix the capital letters, and drop a temporary suffix.
Examples:
' maya '→First: Maya'Chen__temp'→Last: Chen' LUCIA 'and'Lopez__temp'→Second full: Lucia Lopez
Requirements:
- Trim each first name, make it lowercase, then capitalize it.
- Remove
__tempfrom each last name.removeEnd,removeandreplaceall work. - Build each full name from the clean first name, one space, and the clean last name.
- Print
First:,Last:andFull:for case A, thenSecond full:for case B.
Starter code
String firstName = ' maya ';
String lastName = 'Chen__temp';
// Case A: maya / Chen
// Case B: LUCIA / Lopez
firstName = ' LUCIA ';
lastName = 'Lopez__temp';
When it passes
The log shows Maya, Chen and Maya Chen for case A, and Lucia Lopez for case B, with no extra spaces or suffixes.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.