获取中文首字母

package  com.mjm;

/**
 * 功能:取中文的第一个字母 可应用用中文的字典排序 电话本,搜索...
 
*/

public   class  GetCh2Spell  {
    
private String _FromEncode_ = "GBK";
    
private String _ToEncode_ = "GBK";

    
/*
     * compareTo(String anotherString)按字典顺序比较两个字符串。 该比较基于字符串中各个字符的 Unicode 值。 将此
     * String 对象表示的字符序列与参数字符串所表示的字符序列进行比较。 如果按字典顺序此 String
     * 对象在参数字符串之前,则比较结果为一个负整数。 如果按字典顺序此 String
     * 对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0;compareTo 只有在方法 equals(Object)
     * 返回 true 时才返回 0。 这是字典排序的定义。
     * 如果这两个字符串不同,则要么它们在某个索引处具有不同的字符,该索引对二者均为有效索引,要么它们的长度不同,或者同时具备上述两种情况。
     * 如果它们在一个或多个索引位置上具有不同的字符,假设 k 是这类索引的最小值;则按照 < 运算符确定的那个字符串在位置 k
     * 上具有较小的值,其字典顺序在其他字符串之前。 这种情况下,compareTo 返回这两个字符串在位置 k 处的两个不同的 char 值,即值:
     * this.charAt(k)-anotherString.charAt(k)
     * 如果它们没有不同的索引位置,则较短字符串在字典顺序上位于较长字符串的前面。这种情况下,compareTo 返回这两个字符串长度的不同,即值:
     * this.length()-anotherString.length()
     * 
     * compareTo("你好","你好"); 结果:0 compareTo("你","你好"); 结果:-1
     * compareTo("你好","你"); 结果:1 以上,由于对应位置的值都相等,所有返回的是 长度差。
     * 
     * compareTo("你好1","你好2"); 结果:-1 compareTo("你好1","你好9"); 结果:-8 以上返回值是不同位置索引的
     * 差~,即:字符编码的差额
     
*/

    
/*
     * 功能:返回两个字符的字典顺序比较结果
     
*/

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


    
/*
     * 公能:得到一个汉字的首字符
     
*/

    
public String getFirstStr(String str) {
        
char a = str.charAt(0);
        
char aa[] = { a };
        String sss 
= new String(aa);
        
if (Character.isDigit(aa[0]))
            sss 
= "data";
        
else if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
            sss 
= "character";
        
else
            sss 
= getBeginCharacter(sss);
        
return sss;
    }


    
public static void main(String[] args) {
        System.out.println(
new GetCh2Spell().getBeginCharacter("你好吗!"));
        
// System.out.println("啊芭擦搭发啪然期击");
    }

}

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值