栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;  
    }  

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143647.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号