java取得中文的首字母

来源:互联网 发布:微信淘宝返利源码 编辑:程序博客网 时间:2024/05/11 04:41

 

 

取得中文字符的首字母。

 


public class CharAbbreviate {

  
private static final String _FromEncode_ = "GBK";
  
private static final String _ToEncode_ = "GBK";
  
/**
   * 是否过滤特殊字符
   * 
*/

  
private static boolean isOutSpecialCharacter = false;

  
public CharAbbreviate() {
  }


  
public static int compare(String str1, String str2)
     
{
         
int result = 0;
         String m_s1 
= null;
         String m_s2 
= null;
         
try
         
{
             m_s1 
= new String(str1.getBytes(_FromEncode_), _ToEncode_);
             m_s2 
= new String(str2.getBytes(_FromEncode_), _ToEncode_);
         }

         
catch(Exception e)
         
{
             
return str1.compareTo(str2);
         }

         result 
= chineseCompareTo(m_s1, m_s2);
         
return result;
     }


     
public static int getCharCode(String s)
     
{
         
if(s == null && s.equals(""))
             
return -1;
         
byte b[] = s.getBytes();
         
int value = 0;
         
for(int i = 0; i < b.length && i <= 2; i++)
             value 
= value * 100 + b[i];

         
return value;
     }


     
public static int chineseCompareTo(String s1, String s2)
     
{
         
int len1 = s1.length();
         
int len2 = s2.length();
         
int n = Math.min(len1, len2);
         
for(int i = 0; i < n; i++)
         
{
             
int s1_code = getCharCode(s1.charAt(i) + "");
             
int s2_code = getCharCode(s2.charAt(i) + "");
             
if(s1_code * s2_code < 0)
                 
return Math.min(s1_code, s2_code);
             
if(s1_code != s2_code)
                 
return s1_code - s2_code;
         }


         
return len1 - len2;
     }


     
public static String getBeginCharacter(String res)
     
{
         String a 
= res;
         String result 
= "";
         
for(int i = 0; i < a.length(); i++)
         
{
             String current 
= a.substring(i, i + 1);
             
if(compare(current, ""< 0)
                 result 
= result + current;
             
else
             
if(compare(current, "">= 0 && compare(current, ""<= 0)
                 
if(compare(current, "">= 0)
                     result 
= result + "z";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "y";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "x";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "w";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "t";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "s";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "r";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "q";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "p";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "o";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "n";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "m";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "l";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "k";
                 
else
                 
if(compare(current, ""> 0)
                     result 
= result + "j";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "h";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "g";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "f";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "e";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "d";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "c";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "b";
                 
else
                 
if(compare(current, "">= 0)
                     result 
= result + "a";
         }


         
return result;
     }


     
/**取得中文字符的首字母
      * 
@param chineseStr 中文字符
      * 
@return 中文字符串的首字母
      * 
@author 
      
*/

     
public static String getFirstStr(String str)
     
{
         
char a = str.charAt(0);
         
char aa[] = {
             a
         }
;
         String sss 
= new String(aa);
         
if(Character.isDigit(aa[0])){
             
//如果输入了数字将数字返回
             sss = String.valueOf(aa[0]);
         }
else
         
if(a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z'){
             
//如果输入了字母,将原字母返回去
             sss = String.valueOf(a);
         }
else if ( (a >= '[' && a <= ']'|| a == ',' || a == '.' || a == '/'||
               a 
== '*' || (a == '@'|| (a >= '{' && a <= '~'|| a == '' ||
               a 
== '' || a == ';' || a == '%' || a == '&' || a == '#' ||
               a 
== '_' || a == '^' || a == '(' || a == ')' || a == '=' ||
               a 
== '+' || a == '-' || a == '`' || a == '$' || a == '' ||
               a 
== '|' || a == '''{
            
if ( isOutSpecialCharacter )
              sss 
= String.valueOf(a);
            
else
              
return "";
         }
 else {
           sss 
= getBeginCharacter(sss);
        }

         
return sss.toLowerCase();
     }


     
/**取得中文字符串的首字母
      * 
@param chineseStr 中文字符串
      * 
@return 中文字符串的首字母
      * 
@author 
      
*/

     
public static String getAbbreviate(String chineseStr){

       
if (chineseStr==null{
         
return "";
       }

       StringBuffer buffer 
=new StringBuffer();
       
int length = chineseStr.length();

       
for (int i=0;i<length;i++{
         buffer.append(getFirstStr(chineseStr.substring(i,i
+1)));
       }

       
return buffer.toString().toLowerCase();
     }


     
/**取得中文字符串的首字母
      * 
@param chineseStr 中文字符串
      * 
@return 中文字符串的首字母
      * 
@author 
      
*/

     
public static String getAbbreviate(String chineseStr,boolean outSpecialCharacter ){

       
if (chineseStr==null{
         
return "";
       }


       isOutSpecialCharacter 
= outSpecialCharacter;

       StringBuffer buffer 
=new StringBuffer();
       
int length = chineseStr.length();

       
for (int i=0;i<length;i++{
         buffer.append(getFirstStr(chineseStr.substring(i,i
+1)));
       }

       
return buffer.toString().toLowerCase();
     }


}