java中英文获取首字母

package com.syz;
/**
 * @author shiyz
 * @creation date 2012-10-25 上午11:01:34
 *
 */
public class GB2Alpha {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GB2Alpha obj1 = new GB2Alpha();
		System.out.println(obj1.String2Alpha("shiyuezhong测试:中华人民共和国!","b"));//shiyuezhongCS0ZHRMGHG0
		System.out.println(obj1.String2Alpha("程序猿","s")); //cxy
		System.out.println(obj1.String2AlphaFirst("狄仁杰","b")); //D
		System.out.println(obj1.String2AlphaFirst("q","b"));//英文按实际大小写输出  q 参数不起作用
		System.out.println(obj1.String2AlphaFirst("包拯","s")); //b
		System.out.println(obj1.String2AlphaFirst("Kobe","s"));//英文按实际大小写输出  K 参数不起作用
		System.out.println(obj1.gbValue('施'));//51881
	  } 
        public GB2Alpha() {
 	 
        }
	     //字母Z使用了两个标签,这里有27个值
	 	 //i, u, v都不做声母, 跟随前面的字母
	    private char[] chartable =
           {
             '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
             '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
             '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
            };
	    private char[] alphatableb =
          {
 	         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
             'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
 	       };
	    private char[] alphatables =
	          {
	 	         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
	             'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
	 	       };
	    private int[] table = new int[27];  //初始化
	      {
	 	         for (int i = 0; i < 27; ++i) {
	 	             table[i] = gbValue(chartable[i]);
	 	         }
	       }
		//主函数,输入字符,得到他的声母,
		//英文字母返回对应的大小写字母
	    //其他非简体汉字返回 '0'  按参数
	     public char Char2Alpha(char ch,String type) {
	          if (ch >= 'a' && ch <= 'z')
	           // return (char) (ch - 'a' + 'A');
	        	  return ch;
	          if (ch >= 'A' && ch <= 'Z')
	              return ch;

	             int gb = gbValue(ch);
	 	         if (gb < table[0])
	              return '0';
	  
	          int i;
	 	         for (i = 0; i < 26; ++i) {
	              if (match(i, gb))
	 	                 break;
	          }
	 	 
	 	         if (i >= 26){
	              return '0';}
	 	         else{
	 	        	 if("b".equals(type)){//大写
	                     return alphatableb[i];
	                 }else{//小写
	                	 return alphatables[i];
	                 }
	 	         }
	      }
	 //根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
     public String String2Alpha(String SourceStr,String type) {
         String Result = "";
         int StrLength = SourceStr.length();
         int i;
      try {
          for (i = 0; i < StrLength; i++) {
                 Result += Char2Alpha(SourceStr.charAt(i),type);
             }
         } catch (Exception e) {
          Result = "";
         }
      return Result;
    }
   //根据一个包含汉字的字符串返回第一个汉字拼音首字母的字符串
     public String String2AlphaFirst(String SourceStr,String type) {
           String Result = "";
         try {
           Result += Char2Alpha(SourceStr.charAt(0),type);
         } catch (Exception e) {
           Result = "";
         }
      return Result;
    }
     private boolean match(int i, int gb) {
            if (gb < table[i])
               return false;
 	         int j = i + 1;
  	 
 	         //字母Z使用了两个标签
  	         while (j < 26 && (table[j] == table[i]))
 	             ++j;
 	         if (j == 26)
 	             return gb <= table[j];
            else
 	             return gb < table[j];
  	      }
    	  	 
     //取出汉字的编码
     private int gbValue(char ch) {
	     String str = new String();
	     str += ch;
	     try {
	         byte[] bytes = str.getBytes("GBK");
	             if (bytes.length < 2)
	                 return 0;
	             return (bytes[0] << 8 & 0xff00) + (bytes[1] &
	                     0xff);
	         } catch (Exception e) {
	           return 0;
	         }
	     }
}

 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值