英文:
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;
}
- 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.
(?<=.{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("(?<=.{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"));
}
}
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("@")/2;
int endIndex = email.indexOf("@");
int numOfCharsToHide = endIndex - startIndex;
email.replace(startIndex, endIndex, "*".repeat(numOfCharsToHide));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论