带有字符的掩码字符串 [英] Mask String with characters

查看:48
本文介绍了带有字符的掩码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我试图找到一种隐藏字符串的方法,但是我发现的代码仅适用于我的应用程序...有没有一种方法可以使用 * 或-,如果有人可以解释

Hey guy's I tried to find a way to hide a string, but the code that I found just work with my application... Is there a way to hide the characters in a string with either * or - and if there is can someone please explain

推荐答案

这是用于输入密码的吗?请考虑以下内容:

Is this for making a password? Consider the following:

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();
    }
}

或者使用the子手的方法

Or for the hangman approach

class Hangman {
    final String word;
    final BitSet revealed;
    public Hangman(String word) {
        this.word = word;
        this.revealed = new BitSet(word.length());
        reveal(' ');
        reveal('-');
    }
    public void reveal(char c) {
        for(int i = 0; i < word.length; i++) {
            if(word.charAt(i) == c) revealed.set(i);
        }
    }
    public boolean solve(String guess) {
        return word.equals(guess);
    }
    public String toString() {
         StringBuilder sb = new StringBuilder(word.length());
         for(int i = 0; i < word.length; i++) {
             char c = revealed.isSet(i) ? word.charAt(i) : "*";
         }
         return sb.toString();
    }
}

这篇关于带有字符的掩码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆