java 判断标点符号_Java判断中英文符号、标点的实现

本文介绍了Java判断中英文符号、标点的实现,分享给大家,具体如下:

方法一、用unicodeBlock和unicodeScript判断

在Java中,主要使用 Character类处理字符有关功能,而JDK 1.7中Character是按照Unicode 6.0版本实现的,所以这个要先学习下常用的 Unicode编码。

其中的UnicodeBlock 和 UnicodeScript类可以帮助我们判断字符类型,UnicodeBlock是Unicode标准协会组织unicode码的一个基本单位,实际上一个 UnicodeBlock代表一片连续的Unicode号码段,UnicodeBlock之间不重叠。例如,通常我们利用Unicode编码是否在 0x4E00–0x9FCC 来判断某字符是否为汉字,就是因为,有个UnicodeBlock 专门划分为存储汉字 (准确的说是 CJK统一汉字),这个UnicodeBlock叫做 CJK Unified Ideographs,总共定义了 74,617 个汉字。

UnicodeBlock 与 UnicodeScript 关系:

所以UnicodeScript 是从语言书写规则层次对Unicode字符的分类,这是用使用角度划分,而UnicodeBlock是从硬的编码角度划分。

1. UnicodeBlock是简单的数值范围 (其中可能有些Block中会有一些尚未分配字符的“空号”)。

2. 在一个UnicodeScript中的字符可能分散在多个UnicodeBlock中;

3. 一个UnicodeBlock中的字符可能会被划进多个UnicodeScript中。

判别中文标点符号。

因为中文的标点符号主要存在于以下5个UnicodeBlock中,

U2000-General Punctuation (百分号,千分号,单引号,双引号等)

U3000-CJK Symbols and Punctuation ( 顿号,句号,书名号,〸,〹,〺 等;PS: 后面三个字符你知道什么意思吗? : )    )

UFF00-Halfwidth and Fullwidth Forms ( 大于,小于,等于,括号,感叹号,加,减,冒号,分号等等)

UFE30-CJK Compatibility Forms  (主要是给竖写方式使用的括号,以及间断线﹉,波浪线﹌等)

UFE10-Vertical Forms (主要是一些竖着写的标点符号,    等等)

// 根据UnicodeBlock方法判断中文标点符号

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;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值