Java:判断中英文符号、标点
2017-01-10 08:46
375 查看
方法一、用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 (主要是一些竖着写的标点符号, 等等)
方法二、用字符范围判断
参考:http://bbs.csdn.net/topics/390812840
在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; }方法三、自定义标点符号集合,检验(略)
参考:http://bbs.csdn.net/topics/390812840
相关文章推荐
- 判断中英文符号、标点
- java File 判断文件是否为符号链接
- java判断字符串是否全部由数字,大小写字母,特殊符号组成
- JAVA判断中英文,并根据中英文自动截取字符串
- Java通过正则表达式判断字符串当中是否有特殊符号
- java 去除标点等符号
- java实现汉字判断+中文符号判断
- Java删除/清除/过滤标点(所有中英文标点)的正则表达式
- 【java】java中判断一个字符是否包含汉字,包含汉语标点
- java File 判断文件是否为符号链接
- 统计判断中英文汉字符号空格
- Java如何判断字符串中包含有全角,半角符号
- Java的运算符号和判断
- java判断中英文占位
- Java 完美判断字符串中中文字符【中文符号】
- java File 判断文件是否为符号链接
- JAVA判断中文汉字以及中文标点
- Java实现1+2+...+n,不用乘除符号,不用if等条件判断,不用for等循环
- js判断中英文字符串长度
- java判断操作系统