XOR operation with two strings in java

前端 未结 7 550
有刺的猬
有刺的猬 2020-11-28 05:48

How to do bitwise XOR operation to two strings in java.

7条回答
  •  盖世英雄少女心
    2020-11-28 06:25

    the abs function is when the Strings are not the same length so the legth of the result will be the same as the min lenght of the two String a and b

    public String xor(String a, String b){
        StringBuilder sb = new StringBuilder();
        for(int k=0; k < a.length(); k++)
           sb.append((a.charAt(k) ^ b.charAt(k + (Math.abs(a.length() - b.length()))))) ;
           return sb.toString();
    }
    

提交回复
热议问题