在特定索引之后使用符号隐藏字符

huangapple go评论60阅读模式
英文:

Hide characters after certain index with symbols

问题

我理解我做了很多可以避免的硬编码如果有任何人可以分享他们的想法请继续

private String hideEmailCharacters(String privateEmail){
    String emailName = privateEmail.substring(0,privateEmail.indexOf("@"));
    StringBuilder stringBuffer = new StringBuilder(emailName);
    stringBuffer.replace(emailName.length() / 2,emailName.length(), StringUtils.repeat("*", emailName.length() / 2));
    String emailProvider = privateEmail.substring(privateEmail.indexOf("@"));
    return stringBuffer + emailProvider;
}

1) 目标是例如覆盖一半的电子邮件名称或用星号 ** 覆盖第二个字符后的所有内容因此abcdv@example.com 的结果将是 ab***@example.com
英文:

I understand that I did a lot of hard coding that can be avoided so if anything can share their thought please go ahead:

private String hideEmailCharacters(String privateEmail){
    String emailName = privateEmail.substring(0,privateEmail.indexOf("@"));
    StringBuilder stringBuffer = new StringBuilder(emailName);
    stringBuffer.replace(emailName.length() / 2,emailName.length(), StringUtils.repeat("*", emailName.length() / 2));
    String emailProvider = privateEmail.substring(privateEmail.indexOf("@"));
    return stringBuffer + emailProvider;
}
  1. The goal is to cover for example half of the email name or cover everything after the second character with stars ** so the result from abcdv@example.com would be ab***@example.com

答案1

得分: 3

字符串处理很酷,但这里有一个使用正则表达式的解决方案。

public class TestCode {
    private static String hideEmailCharacters(String privateEmail) {
        return privateEmail.replaceAll("(?<=.{2}).(?=[^@]*?@)", "*");
    }

    public static void main(String[] args) {
        System.out.println(hideEmailCharacters("abcdv@example.com"));
        System.out.println(hideEmailCharacters("ra0o29ajzsdc242@example.com"));
        System.out.println(hideEmailCharacters("x2helloyouthere@example.com"));
        System.out.println(hideEmailCharacters("a@foo.com"));
    }
}

输出:

ab***@example.com
ra*************@example.com
x2*************@example.com
a@foo.com
英文:

String processing is cool but here's a solution with Regular Expression.

(?&lt;=.{2}).(?=[^@]*?@)

Intuition:

  • Ignore first 2 characters from the characters before @
  • Ignore @ as well.
  • Replace each character between with *
public class TestCode {
	private static String hideEmailCharacters(String privateEmail) {
		return privateEmail.replaceAll(&quot;(?&lt;=.{2}).(?=[^@]*?@)&quot;, &quot;*&quot;);
	}

	public static void main(String[] args) {
		System.out.println(hideEmailCharacters(&quot;abcdv@example.com&quot;));
		System.out.println(hideEmailCharacters(&quot;ra0o29ajzsdc242@example.com&quot;));
		System.out.println(hideEmailCharacters(&quot;x2helloyouthere@example.com&quot;));
		System.out.println(hideEmailCharacters(&quot;a@foo.com&quot;));
	}
}

Output:

ab***@example.com
ra*************@example.com
x2*************@example.com
a@foo.com

答案2

得分: 2

这段代码将起到作用:

StringBuffer email = new StringBuffer(privateEmail)
int startIndex = email.indexOf("@")/2;
int endIndex = email.indexOf("@");
int numOfCharsToHide = endIndex - startIndex;
email.replace(startIndex, endIndex, "*".repeat(numOfCharsToHide));
英文:

This piece of code will do the trick:

StringBuffer email = new StringBuffer(privateEmail)
int startIndex = email.indexOf(&quot;@&quot;)/2;
int endIndex = email.indexOf(&quot;@&quot;);
int numOfCharsToHide = endIndex - startIndex;
email.replace(startIndex, endIndex, &quot;*&quot;.repeat(numOfCharsToHide));

huangapple
  • 本文由 发表于 2020年9月4日 04:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63731042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定