Drill

ProblemsJava › text

Hide most of an email address

easytextStringsParsingJava

A support screen shows enough of the address for an agent to recognise it, without putting the whole thing on screen.

maskEmail(address: string) → string

Java 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.

Solve it in Python →

Where you start

String maskEmail(String address) {
    
}

Worked examples

CallResult
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 Java
String maskEmail(String address) {
    int at = address.indexOf('@');
    if (at <= 0) return address;
    StringBuilder sb = new StringBuilder();
    sb.append(address.charAt(0));
    for (int i = 1; i < at; i++) sb.append('*');
    return sb + address.substring(at);
}

The same problem in another language

More text problems in Java