Hide most of an email address
A support screen shows enough of the address for an agent to recognise it, without putting the whole thing on screen.
- Keep the first character of the part before the @, replace the rest of that part with asterisks, and keep the domain as it is.
- A single-character local part has nothing to hide, so it comes back unchanged.
- Anything without an @, or with nothing before it, comes back untouched.
maskEmail(address: string) → string
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func maskEmail(address string) string {
}
Worked examples
| Call | Result |
|---|---|
maskEmail("ahmet@example.com") | "a****@example.com" |
maskEmail("a@b.com") | "a@b.com" |
maskEmail("noatsign") | "noatsign" |
maskEmail("@x.com") | "@x.com" |
Hint
Find the @ first. Everything follows from where it is.
Reference solution in Go
func maskEmail(address string) string {
at := strings.IndexByte(address, '@')
if at <= 0 {
return address
}
return address[:1] + strings.Repeat("*", at-1) + address[at:]
}