Given an email address, mask the local part (everything before the @) so only its first and last characters remain visible, replacing every character in between with an asterisk *. The domain part (the @ and everything after it) is never masked.

If the local part is 2 characters or shorter there is nothing to hide, so return the email unchanged. If the input is null or contains no @ character, return an empty string.

Examples:

Input: email = 'john.doe@example.com' Output: 'j******e@example.com' Explanation: The local part 'john.doe' keeps 'j' and 'e' and hides the 6 characters between them Input: email = 'sam@mail.com' Output: 's*m@mail.com' Explanation: Only the single middle character 'a' is hidden Input: email = 'ab@test.org' Output: 'ab@test.org' Explanation: A local part of 2 characters has nothing to hide, so it is returned unchanged Input: email = null Output: '' Explanation: Null input returns an empty string Input: email = 'nodomain' Output: '' Explanation: There is no @ character, so the value is not an email address
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay November 2026 (Nov 1–15)Sponsored by FLXBL

Contest runs November 1 - 15. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In