StringBuilder replacing specific characters I want to mask certain parts of an email address, and not to use regex at all.

Example: 


  "testing@gmail.com" should become "t*****g@g***l.com"


Where the characters in the email (barring the first and last) become masked out, and no change to the domain.

        String email = "testing@gmail.com";
        StringBuilder sb = new StringBuilder(email);
        int mp = sb.lastIndexOf("@");
        int dp = sb.substring(mp).indexOf(".");
        for (int i = 1; i &lt; sb.length(); i++) {
            if (i != mp &amp;&amp; i != mp - 1 &amp;&amp; i != mp + 1 &amp;&amp; i != ((mp + dp) - 1) &amp;&amp; i &lt; (dp + mp)) {
                sb.setCharAt(i, '*');
            }
        }


I know this will fail if something unusual such as an @ in the domain, but that's fine.

It's mainly the if statement that seems awful to me, and wondering if I could shorten it to much simpler logic.

Online Demo
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.