Java - 替换多个字符而不覆盖最后一个字符

时间:2013-07-27 22:01:54

标签: java replace character overwrite

我正在尝试制作某种加密和解密程序,它需要一个字母并将其转换为键盘上的下一个字母(如下所示)

 data = data.replace('q', 'w');
 data = data.replace('w', 'e');

(数据是一个字符串)

使用此代码将“q”变为“w”,然后将“w”变为“e”,我不希望发生这种情况。我怎么能避免这个?

3 个答案:

答案 0 :(得分:0)

这种编码方法称为Caesar密码。简单的谷歌搜索密码可以获得大量的代码片段。为方便起见,我在下面附上了一个代码段。

 class CaesarCipher {
    private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    public String encrypt(String plainText,int shiftKey)
    {
       plainText = plainText.toLowerCase();
       String cipherText="";
       for(int i=0;i<plainText.length();i++)
       {
            int charPosition = ALPHABET.indexOf(plainText.charAt(i));
            int keyVal = (shiftKey+charPosition)%26;
            char replaceVal = this.ALPHABET.charAt(keyVal);
            cipherText += replaceVal;
       }
       return cipherText;
 }
 public String decrypt(String cipherText, int shiftKey)
 {
       cipherText = cipherText.toLowerCase();
       String plainText="";
       for(int i=0;i<cipherText.length();i++)
       {
            int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i));
            int keyVal = (charPosition-shiftKey)%26;
            if(keyVal<0)
            {
                  keyVal = this.ALPHABET.length() + keyVal;
            }
            char replaceVal = this.ALPHABET.charAt(keyVal);
            plainText += replaceVal;
       }
       return plainText;
   }

}

 class CaesarDemo {
 public static void main(String args[])
 {
       String plainText = "studentitzone";
       int shiftKey=4;

       CaesarCipher cc = new CaesarCipher();

       String cipherText = cc.encrypt(plainText,shiftKey);
       System.out.println("Your Plain  Text :" + plainText);
       System.out.println("Your Cipher Text :" + cipherText);

       String cPlainText = cc.decrypt(cipherText,shiftKey);
       System.out.println("Your Plain Text  :" + cPlainText);
 }
}

其中shiftkey的值决定了你需要移动的角色数量。例如,如果shiftkey = 4,则所有A将被D替换。

来源:http://en.wikipedia.org/wiki/Caesar_cipher
         http://beta.studentitzone.com/UI/viewarticle/Caesar-cipher-Encryption-and-Decryption-Program-in-Java

希望这有帮助

答案 1 :(得分:0)

这样可以解决问题:

String data = "...";

StringBuilder finalData = new StringBuilder(data.length());
for(int i = 0; i < data.length() - 1; i++) {
    char replacement = getReplacement(data.charAt(i));
    finalData.append(replacement);
}
finalData.append(data.charAt(data.length() - 1));

String result = finalData.toString();

答案 2 :(得分:0)

看起来你正试图用键盘右侧的键映射键盘上的键。您可以使用HashMap手动将每个键映射到特定字符。为这么多角色添加映射非常繁琐!我不知道如何动态映射它们。

public static void foo(String str) {
    HashMap<Character, Character> map = new HashMap<Character, Character>();
    char c;
    StringBuilder sb = new StringBuilder();

    map.put('q', 'w');
    map.put('w', 'e');
    map.put('e', 'r');
    ... // Add some more mappings here
    ...

    for (int i = 0; i < str.length(); i++) {
    c = str.toLowerCase().charAt(i);
    sb.append(map.get(c));
    }
    String result = sb.toString();
    System.out.println(result);
}