[英]How can I Replace per character in java
I have this task to mask the contents of a .CSV file using Java.我的任务是使用 Java 屏蔽 .CSV 文件的内容。 Done with masking other field but the problem is masking the data in the Primary Key column.
完成屏蔽其他字段,但问题是屏蔽主键列中的数据。 I tried using the code below but it doesn't work.
我尝试使用下面的代码,但它不起作用。 How should I do it?
我该怎么做?
String str = src.replaceAll("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "QLBNAVHTROFSEJMIKWPYGDUCZX");
There are a number of ways you could do this, but regular expressions are not the approach I would choose.有很多方法可以做到这一点,但正则表达式不是我会选择的方法。 I would build a map of character to character and then iterate the characters in a given string building the transposed output with the map (and don't forget digits and lowercase letters).
我会构建一个字符到字符的映射,然后迭代给定字符串中的字符,使用映射构建转置输出(不要忘记数字和小写字母)。 Something like,
就像是,
private static Map<Character, Character> MASK_MAP = new HashMap<>();
static {
String inChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
outChars = "QLBNAVHTROFSEJMIKWPYGDUCZX";
inChars += inChars.toLowerCase() + "0123456789";
outChars += outChars.toLowerCase() + "8652103749";
for (int i = 0; i < inChars.length(); i++) {
MASK_MAP.put(inChars.charAt(i), outChars.charAt(i));
}
}
private static String maskKey(String s) {
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
sb.append(MASK_MAP.get(s.charAt(i)));
}
return sb.toString();
}
And then call然后打电话
String out = maskKey(inputString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.