---
title: "获取中文首字母"
author: "zwfc"
date: "2007-11-24T13:17:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-11-24T13:17:00Z"
id: 470959
url: "https://zwfc.iteye.com/blog/470959"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagecom.mjm;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*功能：取中文的第一个字母可应用用中文的字典排序电话本，搜索...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassGetCh2Spell...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateString\_FromEncode\_="GBK";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateString\_ToEncode\_="GBK";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*compareTo(StringanotherString)按字典顺序比较两个字符串。该比较基于字符串中各个字符的Unicode值。将此  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*String对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典顺序此String  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*对象在参数字符串之前，则比较结果为一个负整数。如果按字典顺序此String  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*对象位于参数字符串之后，则比较结果为一个正整数。如果这两个字符串相等，则结果为0；compareTo只有在方法equals(Object)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*返回true时才返回0。这是字典排序的定义。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*如果这两个字符串不同，则要么它们在某个索引处具有不同的字符，该索引对二者均为有效索引，要么它们的长度不同，或者同时具备上述两种情况。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*如果它们在一个或多个索引位置上具有不同的字符，假设k是这类索引的最小值；则按照&lt;运算符确定的那个字符串在位置k  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*上具有较小的值，其字典顺序在其他字符串之前。这种情况下，compareTo返回这两个字符串在位置k处的两个不同的char值，即值：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*this.charAt(k)-anotherString.charAt(k)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*如果它们没有不同的索引位置，则较短字符串在字典顺序上位于较长字符串的前面。这种情况下，compareTo返回这两个字符串长度的不同，即值：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*this.length()-anotherString.length()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*compareTo("你好","你好");结果：0compareTo("你","你好");结果：－1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*compareTo("你好","你");结果：1以上，由于对应位置的值都相等，所有返回的是长度差。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*compareTo("你好1","你好2");结果：－1compareTo("你好1","你好9");结果：－8以上返回值是不同位置索引的  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*差～，即：字符编码的差额  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*功能：返回两个字符的字典顺序比较结果  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicintcompare(Stringstr1,Stringstr2)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intresult=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringm\_s1=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringm\_s2=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)m\_s1=newString(str1.getBytes(\_FromEncode\_),\_ToEncode\_);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)m\_s2=newString(str2.getBytes(\_FromEncode\_),\_ToEncode\_);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnstr1.compareTo(str2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=chineseCompareTo(m\_s1,m\_s2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnresult;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticintgetCharCode(Strings)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(s==null&&s.equals(""))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return-1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byteb\[]=s.getBytes();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intvalue=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;b.length&&i&lt;=2;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)value=value\*100+b\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnvalue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticintchineseCompareTo(Strings1,Strings2)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intlen1=s1.length();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intlen2=s2.length();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intn=Math.min(len1,len2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=0;i&lt;n;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ints1\_code=getCharCode(s1.charAt(i)+"");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ints2\_code=getCharCode(s2.charAt(i)+"");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(s1\_code\*s2\_code&lt;0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnMath.min(s1\_code,s2\_code);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(s1\_code!=s2\_code)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returns1\_code-s2\_code;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnlen1-len2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*功能：返回字符串的首字母  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetBeginCharacter(Stringres)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringa=res;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringresult="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=0;i&lt;a.length();i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringcurrent=a.substring(i,i+1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(compare(current,"啊")&lt;0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+current;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"啊")&gt;=0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)&&compare(current,"座")&lt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(compare(current,"匝")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"z";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"压")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"y";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"昔")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"x";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"挖")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"w";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"塌")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"t";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"撒")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"s";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"然")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"r";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"期")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"q";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"啪")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"p";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"哦")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"o";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"拿")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"n";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"妈")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"m";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"垃")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"l";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"喀")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"k";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"击")&gt;0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"j";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"哈")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"h";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"噶")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"g";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"发")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"f";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"蛾")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"e";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"搭")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"d";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"擦")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"c";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"芭")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"b";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(compare(current,"啊")&gt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)result=result+"a";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnresult;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*公能:得到一个汉字的首字符  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetFirstStr(Stringstr)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)chara=str.charAt(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)charaa\[]=...{a};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringsss=newString(aa);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(Character.isDigit(aa\[0]))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sss="data";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(a&gt;='a'&&a&lt;='z'||a&gt;='A'&&a&lt;='Z')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sss="character";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sss=getBeginCharacter(sss);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnsss;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println(newGetCh2Spell().getBeginCharacter("你好吗！"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//System.out.println("啊芭擦搭发啪然期击");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)