服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - Java判断中英文符号、标点的实现

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

2021-01-22 10:53CS青雀 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 (主要是一些竖着写的标点符号,    等等)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
// 根据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;
    }
  }

方法二、用字符范围判断

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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://blog.csdn.net/ztf312/article/details/54310542

延伸 · 阅读

精彩推荐