ApexSensei

Drills › Write Apex

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:

  1. Trim each first name, make it lowercase, then capitalize it.
  2. Remove __temp from each last name. removeEnd, remove and replace all work.
  3. Build each full name from the clean first name, one space, and the clean last name.
  4. Print First:, Last: and Full: for case A, then Second 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.

Try this drill free

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

‹ True, false, and null · Parse an email ›