StringBuilder replacing specific characters
It's two consecutive ranges that you want to get replaced with *. So, I'd do two loops like:

    for (int i = 1; i &lt; mp-1; i++) {
        sb.setCharAt(i, '*');
    }
    for (int i = mp+2; i &lt; mp+dp-1; i++) {
        sb.setCharAt(i, '*');
    }


And add some sanity checks that the @ and . characters are present.