今天讀取solr里面的數據,往mysql插入時報錯, Incorrect string value: '\xF0\x9F\x93\x8D\xE8\x88...' for column 'title' at row 1

原因是標題有Emoj表情相關字符,因為我這邊不需要保留Emoj表情,不需要復原顯示,所以我選擇了簡單的方式,過濾字符。
byte[] b_text=title3.getBytes();
for (int i = 0; i < b_text.length; i++)
{
if((b_text[i] & 0xF8)== 0xF0){
for (int j = 0; j < 4; j++) {
b_text[i+j]=0x30;
}
i+=3;
}
}
String title=new String(b_text);
title.replace("0000","")
在網上還看到別的代碼,不過我沒有試,你們可以看看
/**
* 將emoji表情替換成空串
*
* @param source
* @return 過濾后的字符串
*/
public static String filterEmoji(String source) {
if (source != null && source.length() > 0) {
return source.replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", "");
} else {
return source;
}
}</span></span>
