Q:
在使用CSDN MarkDown时会出现出现乱码的情况。如下
1⃣️ 和 1️⃣
这两个在Apple输入法中显示相同内容,但是后者在mardown中正常显示,前者成了乱码。
前者是中文输入法中的颜文字,后者则是control + command +space 调出的颜文字框自带文字。
经过Java编码,我们可以发现两个表情编码均是UTF-8
public class Solution {
public static void main(String[] args) {
System.out.println(getEncoding("1⃣️"));
System.out.println(getEncoding("1️⃣"));
String a = new String("1⃣️");
String b = new String("1️⃣");
String c = new String("1⃣️");
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(c.hashCode());
}
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (isEncoding(str, encode)) { // 判断是不是GB2312
return encode;
}
} catch (Exception exception) {
}
encode = "ISO-8859-1";
try {
if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1
return encode;
}
} catch (Exception exception1) {
}
encode = "UTF-8";
try {
if (isEncoding(str, encode)) { // 判断是不是UTF-8
return encode;
}
} catch (Exception exception2) {
}
encode = "GBK";
try {
if (isEncoding(str, encode)) { // 判断是不是GBK
return encode;
}
} catch (Exception exception3) {
}
return ""; // 如果都不是,说明输入的内容不属于常见的编码格式。
}
public static boolean isEncoding(String str, String encode) {
try {
if (str.equals(new String(str.getBytes(), encode))) {
return true;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
}
但是当我们获取两个字符串的hashcode时,我们就会发现,两者的hashcode并不相同。同时,我们去转换颜文字为utf-8编码时,两者的编码也不尽相同。
1⃣️
1️⃣
综上,在markdown中使用颜文字时还是直接从颜文字框中调用较好,尽量不要从中文输入法中调用。
Solution
1.不使用中文输入法显然不现实,但是可以尝试使用第三方输入法。
2.通过 control + command + space显然太麻烦 ,我们可以讲调出颜文字的快捷键设置成单击Fn