如何在java中屏蔽一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18014698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mask a value in java?
提问by ACP
I have a pinNumber value. How to mask it (ie) say if my pinNumber is 1234 and mask it with four asteriks symbol instead of showing the numbers. This masked value will be shown in a webpage using a jsp.
我有一个 pinNumber 值。如何屏蔽它(即)说如果我的 pinNumber 是 1234 并用四个星号符号屏蔽它而不是显示数字。此屏蔽值将显示在使用 jsp 的网页中。
采纳答案by Graham Griffiths
At some point in your code, you will convert the number to a string for display. At this point, you can simply call String's replaceAll method to replace all the characters from 0-9 with * character :
在代码中的某个时刻,您会将数字转换为字符串以进行显示。此时,您可以简单地调用 String 的 replaceAll 方法将 0-9 中的所有字符替换为 * 字符:
s.replaceAll("[0-9]", "*")
well - this is how you do it directly in Java. In JSP it is taken care of for you, as other posters show, if you select 'password' type on your input.
好吧 - 这就是您直接在 Java 中执行此操作的方式。在 JSP 中,正如其他海报所示,如果您在输入中选择“密码”类型,它会为您处理。
回答by Sajal Dutta
<input type="PASSWORD" name="password">
If you are going to display a password on a label, just display 5 or 6 asterisk characters as you do not want to give them clues by making the length of that label with the length of the password.
如果您要在标签上显示密码,只需显示 5 或 6 个星号字符,因为您不想通过将标签的长度与密码的长度相同来给他们提供线索。
回答by Mohsin Shaikh
You create form like that
你创建这样的表格
<form action="something" method="post">
pinNumber:<input type="password" name="pass"/>
<input type="submit" value="OK"/>
</form>
then it will change the into *
然后它会变成*
回答by Rahul Kulhari
This code might help you.
此代码可能对您有所帮助。
class Password {
final String password; // the string to mask
Password(String password) { this.password = password; } // needs null protection
// allow this to be equal to any string
// reconsider this approach if adding it to a map or something?
public boolean equals(Object o) {
return password.equals(o);
}
// we don't need anything special that the string doesnt
public int hashCode() { return password.hashCode(); }
// send stars if anyone asks to see the string - consider sending just
// "******" instead of the length, that way you don't reveal the password's length
// which might be protected information
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; < password.length(); i++)
sb.append("*");
return sb.toString();
}
}
回答by Rahul Kulhari
I use following code in my many application and it works nicely.
我在我的许多应用程序中使用以下代码,它运行良好。
public class Test {
public static void main(String[] args) {
String number = "1234";
StringBuffer maskValue = new StringBuffer();
if(number != null && number.length() >0){
for (int i = 0; i < number.length(); i++) {
maskValue.append("*");
}
}
System.out.println("Masked Value of 1234 is "+maskValue);
}
}
Ans - Masked Value of 1234 is ****
回答by SoSen
Two ways (this will mask all charecters not only numbers) :
两种方式(这将掩盖所有字符,而不仅仅是数字):
private String mask(String value) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < value.length(); sb.append("*"), i++);
return sb.toString();
}
or:
或者:
private String mask(String value) {
return value.replaceAll(".", "*");
}
I use the second one
我用第二个