当前位置: 移动技术网 > IT编程>开发语言>Java > Java判断中英文符号、标点的实现

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文介绍了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;  
    }  

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网