Java判斷中英文符號(hào)、標(biāo)點(diǎn)的實(shí)現(xiàn)
本文介紹了Java判斷中英文符號(hào)、標(biāo)點(diǎn)的實(shí)現(xiàn),分享給大家,具體如下:
方法一、用unicodeBlock和unicodeScript判斷
在Java中,主要使用 Character類處理字符有關(guān)功能,而JDK 1.7中Character是按照Unicode 6.0版本實(shí)現(xiàn)的,所以這個(gè)要先學(xué)習(xí)下常用的 Unicode編碼。
其中的UnicodeBlock 和 UnicodeScript類可以幫助我們判斷字符類型,UnicodeBlock是Unicode標(biāo)準(zhǔn)協(xié)會(huì)組織unicode碼的一個(gè)基本單位,實(shí)際上一個(gè) UnicodeBlock代表一片連續(xù)的Unicode號(hào)碼段,UnicodeBlock之間不重疊。例如,通常我們利用Unicode編碼是否在 0x4E00–0x9FCC 來判斷某字符是否為漢字,就是因?yàn)?,有個(gè)UnicodeBlock 專門劃分為存儲(chǔ)漢字 (準(zhǔn)確的說是 CJK統(tǒng)一漢字),這個(gè)UnicodeBlock叫做 CJK Unified Ideographs,總共定義了 74,617 個(gè)漢字。
UnicodeBlock 與 UnicodeScript 關(guān)系:
所以UnicodeScript 是從語言書寫規(guī)則層次對(duì)Unicode字符的分類,這是用使用角度劃分,而UnicodeBlock是從硬的編碼角度劃分。
1. UnicodeBlock是簡單的數(shù)值范圍 (其中可能有些Block中會(huì)有一些尚未分配字符的“空號(hào)”)。
2. 在一個(gè)UnicodeScript中的字符可能分散在多個(gè)UnicodeBlock中;
3. 一個(gè)UnicodeBlock中的字符可能會(huì)被劃進(jìn)多個(gè)UnicodeScript中。
判別中文標(biāo)點(diǎn)符號(hào)。
因?yàn)橹形牡臉?biāo)點(diǎn)符號(hào)主要存在于以下5個(gè)UnicodeBlock中,
U2000-General Punctuation (百分號(hào),千分號(hào),單引號(hào),雙引號(hào)等)
U3000-CJK Symbols and Punctuation ( 頓號(hào),句號(hào),書名號(hào),〸,〹,〺 等;PS: 后面三個(gè)字符你知道什么意思嗎? : ) )
UFF00-Halfwidth and Fullwidth Forms ( 大于,小于,等于,括號(hào),感嘆號(hào),加,減,冒號(hào),分號(hào)等等)
UFE30-CJK Compatibility Forms (主要是給豎寫方式使用的括號(hào),以及間斷線﹉,波浪線﹌等)
UFE10-Vertical Forms (主要是一些豎著寫的標(biāo)點(diǎn)符號(hào), 等等)
// 根據(jù)UnicodeBlock方法判斷中文標(biāo)點(diǎn)符號(hào)
public boolean isChinesePunctuation(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
|| ub == Character.UnicodeBlock.VERTICAL_FORMS) {
return true;
} else {
return false;
}
}
方法二、用字符范圍判斷
static boolean isSymbol(char ch)
{
if(isCnSymbol(ch)) return true;
if(isEnSymbol(ch))return true;
if(0x2010 <= ch && ch <= 0x2017) return true;
if(0x2020 <= ch && ch <= 0x2027) return true;
if(0x2B00 <= ch && ch <= 0x2BFF) return true;
if(0xFF03 <= ch && ch <= 0xFF06) return true;
if(0xFF08 <= ch && ch <= 0xFF0B) return true;
if(ch == 0xFF0D || ch == 0xFF0F) return true;
if(0xFF1C <= ch && ch <= 0xFF1E) return true;
if(ch == 0xFF20 || ch == 0xFF65) return true;
if(0xFF3B <= ch && ch <= 0xFF40) return true;
if(0xFF5B <= ch && ch <= 0xFF60) return true;
if(ch == 0xFF62 || ch == 0xFF63) return true;
if(ch == 0x0020 || ch == 0x3000) return true;
return false;
}
static boolean isCnSymbol(char ch) {
if (0x3004 <= ch && ch <= 0x301C) return true;
if (0x3020 <= ch && ch <= 0x303F) return true;
return false;
}
static boolean isEnSymbol(char ch){
if (ch == 0x40) return true;
if (ch == 0x2D || ch == 0x2F) return true;
if (0x23 <= ch && ch <= 0x26) return true;
if (0x28 <= ch && ch <= 0x2B) return true;
if (0x3C <= ch && ch <= 0x3E) return true;
if (0x5B <= ch && ch <= 0x60) return true;
if (0x7B <= ch && ch <= 0x7E) return true;
return false;
}
static boolean isPunctuation(char ch){
if(isCjkPunc(ch)) return true;
if(isEnPunc(ch)) return true;
if(0x2018 <= ch && ch <= 0x201F) return true;
if(ch == 0xFF01 || ch == 0xFF02) return true;
if(ch == 0xFF07 || ch == 0xFF0C) return true;
if(ch == 0xFF1A || ch == 0xFF1B) return true;
if(ch == 0xFF1F || ch == 0xFF61) return true;
if(ch == 0xFF0E) return true;
if(ch == 0xFF65) return true;
return false;
}
static boolean isEnPunc(char ch){
if (0x21 <= ch && ch <= 0x22) return true;
if (ch == 0x27 || ch == 0x2C) return true;
if (ch == 0x2E || ch == 0x3A) return true;
if (ch == 0x3B || ch == 0x3F) return true;
return false;
}
static boolean isCjkPunc(char ch){
if (0x3001 <= ch && ch <= 0x3003) return true;
if (0x301D <= ch && ch <= 0x301F) return true;
return false;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java設(shè)置httponly?cookie的實(shí)現(xiàn)示例
本文主要介紹了Java設(shè)置httponly?cookie的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java實(shí)現(xiàn)布隆過濾器的幾種方式總結(jié)
這篇文章給大家總結(jié)了幾種Java實(shí)現(xiàn)布隆過濾器的方式,手動(dòng)硬編碼實(shí)現(xiàn),引入Guava實(shí)現(xiàn),引入hutool實(shí)現(xiàn),通過redis實(shí)現(xiàn)等幾種方式,文中有詳細(xì)的代碼和圖解,需要的朋友可以參考下2023-07-07
CountDownLatch和Atomic原子操作類源碼解析
這篇文章主要為大家介紹了CountDownLatch和Atomic原子操作類的源碼解析以及理解應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
springboot執(zhí)行延時(shí)任務(wù)之DelayQueue的使用詳解
DelayQueue是一個(gè)無界阻塞隊(duì)列,只有在延遲期滿時(shí),才能從中提取元素。這篇文章主要介紹了springboot執(zhí)行延時(shí)任務(wù)-DelayQueue的使用,需要的朋友可以參考下2019-12-12
Java的ConcurrentLinkedQueue源碼分析
這篇文章主要介紹了Java的ConcurrentLinkedQueue源碼分析,ConcurrentLinkedQueue 是一個(gè)基于鏈接節(jié)點(diǎn)的無界線程安全的隊(duì)列,當(dāng)我們添加一個(gè)元素的時(shí)候,它會(huì)添加到隊(duì)列的尾部,當(dāng)我們獲取一個(gè)元素時(shí),它會(huì)返回隊(duì)列頭部的元素,需要的朋友可以參考下2023-12-12
關(guān)于MyBaties的基本配置標(biāo)簽總結(jié)
今天給大家?guī)淼氖顷P(guān)于MyBaties基礎(chǔ)的相關(guān)知識(shí),文章圍繞著MyBaties的基本配置標(biāo)簽展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06

