您好,登錄后才能下訂單哦!
Java中怎么過濾中文亂碼,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1. Unicode編碼
Unicode編碼是一種涵蓋了世界上所有語言、標(biāo)點等字符的編碼方式,簡單一點說,就是一種通用的世界碼;其編碼范圍:U+0000 .. U+10FFFF。按Unicode硬編碼的區(qū)間進(jìn)行劃分,Unicode編碼被分成若干個block ( Unicode block);每一個Unicode編碼專屬于唯一的Unicode block,Unicode block之間互不重疊。從碼字的本身的屬性出發(fā),Unicode編碼被分成了若干script ( Unicode script);比如,與中文相關(guān)的字符、標(biāo)點的scriptHan包括block如下:
CJK Radicals Supplement
Kangxi Radicals
CJK Symbols and Punctuation中的15個字符
CJK Unified Ideographs Extension A
CJK Unified Ideographs
CJK Compatibility Ideographs
CJK Unified Ideographs Extension B
CJK Unified Ideographs Extension C
CJK Unified Ideographs Extension D
CJK Unified Ideographs Extension E
CJK Compatibility Ideographs Supplement
其中,常見的中文字符在CJK Unified Ideographs block;此外,考慮繁體字及不常見字等,CJK還有A、B、C、D、E五個extension。Basic Latin block完整地包含了ASCII碼的控制字符、標(biāo)點字符與英文字母字符。
2. Java的字符編碼
JDK完整實現(xiàn)Unicode的block與script:
Char c = '?' Character.UnicodeBlock ub = Character.UnicodeBlock.of(c) Character.UnicodeScript uc = Character.UnicodeScript.of(c);
Java中的字符char內(nèi)置的編碼方式是UTF-16,當(dāng)char強轉(zhuǎn)成int類型時,其返回值是unicode編碼值,只有當(dāng)getbyte時才返回的是utf-8編碼的byte:
String s = "\u00a0"; String.format("\\u%04x", (int) s.charAt(0)) // --> \u00a0 import org.apache.commons.codec.binary.Hex; Hex.encodeHex(s.getBytes()) // --> c2a0
UTF-8是Unicode字符的變長前綴編碼的一種實現(xiàn),二者之間的對應(yīng)關(guān)系在這里.現(xiàn)在我們回到開篇過濾中文亂碼的問題,有一個基本解決思路:
去掉各種標(biāo)點字符、控制字符,
計算剩下字符中非中文字符所占的比例,如果超過閾值,則認(rèn)為該字符串為亂碼串
完整代碼如下:
public class ChineseUtill { private static boolean isChinese(char c) { Character.UnicodeScript sc = Character.UnicodeScript.of(c); if (sc == Character.UnicodeScript.HAN) { return true; } return false; } public static boolean isPunctuation(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if ( // punctuation, spacing, and formatting characters ub == Character.UnicodeBlock.GENERAL_PUNCTUATION // symbols and punctuation in the unified Chinese, Japanese and Korean script || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION // fullwidth character or a halfwidth character || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS // vertical glyph variants for east Asian compatibility || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS // vertical punctuation for compatibility characters with the Chinese Standard GB 18030 || ub == Character.UnicodeBlock.VERTICAL_FORMS // ascii || ub == Character.UnicodeBlock.BASIC_LATIN ) { return true; } else { return false; } } private static Boolean isUserDefined(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.NUMBER_FORMS || ub == Character.UnicodeBlock.ENCLOSED_ALPHANUMERICS || ub == Character.UnicodeBlock.LETTERLIKE_SYMBOLS || c == '\ufeff' || c == '\u00a0' ) return true; return false; } public static Boolean isMessy(String str) { float chlength = 0; float count = 0; for(int i = 0; i < str.length(); i++) { char c = str.charAt(i); if(isPunctuation(c) || isUserDefined(c)) continue; else { if(!isChinese(c)) { count = count + 1; } chlength ++; } } float result = count / chlength; if(result > 0.3) return true; return false; } }
關(guān)于Java中怎么過濾中文亂碼問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。