Java判斷中英文符號(hào)、標(biāo)點(diǎn)的實(shí)現(xiàn)
本文介紹了Java判斷中英文符號(hào)、標(biāo)點(diǎn)的實(shí)現(xiàn),分享給大家,具體如下:
方法一、用unicodeBlock和unicodeScript判斷
在Java中,主要使用 Character類(lèi)處理字符有關(guān)功能,而JDK 1.7中Character是按照Unicode 6.0版本實(shí)現(xiàn)的,所以這個(gè)要先學(xué)習(xí)下常用的 Unicode編碼。
其中的UnicodeBlock 和 UnicodeScript類(lèi)可以幫助我們判斷字符類(lèi)型,UnicodeBlock是Unicode標(biāo)準(zhǔn)協(xié)會(huì)組織unicode碼的一個(gè)基本單位,實(shí)際上一個(gè) UnicodeBlock代表一片連續(xù)的Unicode號(hào)碼段,UnicodeBlock之間不重疊。例如,通常我們利用Unicode編碼是否在 0x4E00–0x9FCC 來(lái)判斷某字符是否為漢字,就是因?yàn)?,有個(gè)UnicodeBlock 專(zhuān)門(mén)劃分為存儲(chǔ)漢字 (準(zhǔn)確的說(shuō)是 CJK統(tǒng)一漢字),這個(gè)UnicodeBlock叫做 CJK Unified Ideographs,總共定義了 74,617 個(gè)漢字。
UnicodeBlock 與 UnicodeScript 關(guān)系:
所以UnicodeScript 是從語(yǔ)言書(shū)寫(xiě)規(guī)則層次對(duì)Unicode字符的分類(lèi),這是用使用角度劃分,而UnicodeBlock是從硬的編碼角度劃分。
1. UnicodeBlock是簡(jiǎn)單的數(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),書(shū)名號(hào),〸,〹,〺 等;PS: 后面三個(gè)字符你知道什么意思嗎? : ) )
UFF00-Halfwidth and Fullwidth Forms ( 大于,小于,等于,括號(hào),感嘆號(hào),加,減,冒號(hào),分號(hào)等等)
UFE30-CJK Compatibility Forms (主要是給豎寫(xiě)方式使用的括號(hào),以及間斷線(xiàn)﹉,波浪線(xiàn)﹌等)
UFE10-Vertical Forms (主要是一些豎著寫(xiě)的標(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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單員工管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單員工管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
SpringBoot過(guò)濾器與攔截器深入分析實(shí)現(xiàn)方法
大家應(yīng)該都曉得實(shí)現(xiàn)過(guò)濾器需要實(shí)現(xiàn) javax.servlet.Filter 接口,而攔截器會(huì)在處理指定請(qǐng)求之前和之后進(jìn)行相關(guān)操作,配置攔截器需要兩步,本文通過(guò)實(shí)例代碼給大家介紹SpringBoot 過(guò)濾器和攔截器的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-11-11
Java調(diào)用ChatGPT的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java調(diào)用ChatGPT的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
java中1+1d/5和1+1/5的區(qū)別說(shuō)明
這篇文章主要介紹了java中1+1d/5和1+1/5的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java超詳細(xì)教你寫(xiě)一個(gè)網(wǎng)絡(luò)購(gòu)書(shū)系統(tǒng)案例
這篇文章主要介紹了怎么用Java來(lái)寫(xiě)一個(gè)購(gòu)書(shū)系統(tǒng),購(gòu)買(mǎi)書(shū)籍主要需要每本書(shū)的編號(hào)、書(shū)名、單價(jià)、庫(kù)存屬性,能夠讓客戶(hù)通過(guò)編號(hào)來(lái)選書(shū),感興趣的朋友跟隨文章往下看看吧2022-03-03
SpringBoot?Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)
我們通過(guò)Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進(jìn)行切面的處理,進(jìn)而統(tǒng)計(jì)方法訪問(wèn)的次數(shù)等功能,本文主要介紹了SpringBoot?Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)2024-02-02

