返回故事
我基本上从数据库中检索字符串。我更改了一些文本或那些字符串。然后我将这些字符串上传回数据库,替换原始字符串。查看显示这些字符串的前端后,我注意到了字符问题。我不再有原始字符串,但我确实有更新的字符串。
问题
这些字符串中包含来自其他语言的字符。它们现在无法正确显示。我查看了代码点,原来的章程似乎是一个代码点,现在是两个不同的代码点。
"Je?ro^me" //code-points 8. Code-points: 74, 101, 63, 114, 111, 94, 109, 101
"Jéróme" //code-points 6. Code-points: 74, 233, 114, 243, 109, 101
问题
我怎么"Je?ro^me"
回去"Jéróme"
?
我尝试过的事情
- 使用 Notepad++ 将编码转换为
UTF8
、ANSI
和WINDOWS-1252
. - 创建了一个 Map 来查找类似的东西
e?
并将它们转换为é
.
两个尝试解决问题的问题
一种。尝试不同的转换后问题仍然存在。
湾。这里有两个问题:
- 我不知道要寻找的所有潜力
e?
,等。o^
有超过 20,000 个文件可能涵盖多种语言。 - 如果我有一个以结尾的句子怎么办
e?
我研究了一些东西以更好地理解这个问题
- 什么是 Java 中的“代理对”?
- https://docs.oracle.com/javase/tutorial/i18n/text/supplementaryChars.html
- https://www.w3.org/International/questions/qa-what-is-encoding
- https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/
MCVE
import java.util.HashMap;
import java.util.Map;
/**
*https://stackoverflow.com/questions/5903008/what-is-a-surrogate-pair-in-java
*https://docs.oracle.com/javase/tutorial/i18n/text/supplementaryChars.html
*https://www.w3.org/International/questions/qa-what-is-encoding
*https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/
* @author sedri
*/
public class App {
static String outputString;
public static void main(String[] args) {
//May approach to fix the issue
//Use a map to replace string issue with the correct character
//The output looks good, but I would need to include all special characters for many languages.
//What if I have a sentence like: How old are thee?
Map<String, String> map = new HashMap();
map.put("e?", "é");
map.put("o^", "ó");
final String string = "Je?ro^me";
final String accentString = "Jéróme";
outputString = string;
map.forEach((t, u) -> {
if(outputString.contains(t))
{
outputString = outputString.replace(t, u);
}
});
System.out.println("Fixed output: " + outputString);
System.out.println("");
//End of my attempt at a solution.
System.out.println("code points: " + string.codePoints().count());
for(int i = 0; i < string.length(); i++)
{
System.out.println(string.charAt(i) + ": " + Character.codePointAt(string, i));
}
System.out.println("");
System.out.println("code points: " + accentString.codePoints().count());
for(int i = 0; i < accentString.length(); i++)
{
System.out.println(accentString.charAt(i) + ": " + Character.codePointAt(accentString, i));
}
System.out.println("");
System.out.println("code points: " + outputString.codePoints().count());
for(int i = 0; i < outputString.length(); i++)
{
System.out.println(outputString.charAt(i) + ": " + Character.codePointAt(outputString, i));
}
System.out.println("");
}
}