我打算将普通字符串替换为分割字符串,但是普通字符串之间存在长度差异,为 62,而分割长度结果为 117,所以当我们写入 'a' 按钮时不会更改为“𝕒”是否有另一种更容易编写替换字符串的方法?
public static String doublestruck(String input){
String normal = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String split = "𝟘𝟙𝟚𝟛𝟜𝟝𝟞𝟟𝟠𝟡𝕒𝕓𝕔𝕕𝕖𝕗𝕘𝕙𝕚𝕛𝕟𝕠𝕡𝕢𝕣𝕤𝕥𝕦𝕧𝕨𝕩𝕪𝕫𝔸𝔹ℂ𝔻𝔼𝔽𝔾ℍ𝕀𝕁𝕂𝕃𝕄ℕ𝕆ℙℚℝ𝕊𝕋𝕌𝕍𝕎𝕏𝕐ℤ";
String output = "";
char letter;
for(int i = 0; i < input.length(); i++){
letter = input.charAt(i);
int a = normal.indexOf(letter);
output += (a != -1) ? split.charAt(a):letter;
}
return new StringBuilder(output).toString();
}
最佳答案
像 𝟜 (U+1D7DC) 这样的字母不在 the Basic Multilingual Pane 中。因此在 Java 中占用两个 char
值。
您需要使用 codePointAt
而不是 charAt
要找到正确的偏移量,您需要使用 offsetByCodePoint
而不是直接使用相同的索引。因此 split.charAt(a)
需要替换为 split.codePointAt(spli.offsetByCodePoint(0, a))
。
关于java - 如何用 unicode 字符替换 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60786834/