获取中文首字母

package com.mjm;

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

public class GetCh2Spell ... {
privateString_FromEncode_="GBK";
privateString_ToEncode_="GBK";

/**//*
*compareTo(StringanotherString)按字典顺序比较两个字符串。该比较基于字符串中各个字符的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("你好","你好");结果:0compareTo("你","你好");结果:-1
*compareTo("你好","你");结果:1以上,由于对应位置的值都相等,所有返回的是长度差。
*
*compareTo("你好1","你好2");结果:-1compareTo("你好1","你好9");结果:-8以上返回值是不同位置索引的
*差~,即:字符编码的差额
*/

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

publicintcompare(Stringstr1,Stringstr2)...{
intresult=0;
Stringm_s1
=null;
Stringm_s2
=null;
try...{
m_s1
=newString(str1.getBytes(_FromEncode_),_ToEncode_);
m_s2
=newString(str2.getBytes(_FromEncode_),_ToEncode_);
}
catch(Exceptione)...{
returnstr1.compareTo(str2);
}

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


/**//*
*
*/

publicstaticintgetCharCode(Strings)...{
if(s==null&&s.equals(""))
return-1;
byteb[]=s.getBytes();
intvalue=0;
for(inti=0;i<b.length&&i<=2;i++)
value
=value*100+b[i];

returnvalue;
}


publicstaticintchineseCompareTo(Strings1,Strings2)...{
intlen1=s1.length();
intlen2=s2.length();
intn=Math.min(len1,len2);
for(inti=0;i<n;i++)...{
ints1_code=getCharCode(s1.charAt(i)+"");
ints2_code=getCharCode(s2.charAt(i)+"");
if(s1_code*s2_code<0)
returnMath.min(s1_code,s2_code);
if(s1_code!=s2_code)
returns1_code-s2_code;
}


returnlen1-len2;
}


/**//*
*功能:返回字符串的首字母
*/

publicStringgetBeginCharacter(Stringres)...{
Stringa
=res;
Stringresult
="";
for(inti=0;i<a.length();i++)...{
Stringcurrent
=a.substring(i,i+1);
if(compare(current,"")<0)
result
=result+current;
elseif(compare(current,"")>=0
&&compare(current,"")<=0)
if(compare(current,"")>=0)
result
=result+"z";
elseif(compare(current,"")>=0)
result
=result+"y";
elseif(compare(current,"")>=0)
result
=result+"x";
elseif(compare(current,"")>=0)
result
=result+"w";
elseif(compare(current,"")>=0)
result
=result+"t";
elseif(compare(current,"")>=0)
result
=result+"s";
elseif(compare(current,"")>=0)
result
=result+"r";
elseif(compare(current,"")>=0)
result
=result+"q";
elseif(compare(current,"")>=0)
result
=result+"p";
elseif(compare(current,"")>=0)
result
=result+"o";
elseif(compare(current,"")>=0)
result
=result+"n";
elseif(compare(current,"")>=0)
result
=result+"m";
elseif(compare(current,"")>=0)
result
=result+"l";
elseif(compare(current,"")>=0)
result
=result+"k";
elseif(compare(current,"")>0)
result
=result+"j";
elseif(compare(current,"")>=0)
result
=result+"h";
elseif(compare(current,"")>=0)
result
=result+"g";
elseif(compare(current,"")>=0)
result
=result+"f";
elseif(compare(current,"")>=0)
result
=result+"e";
elseif(compare(current,"")>=0)
result
=result+"d";
elseif(compare(current,"")>=0)
result
=result+"c";
elseif(compare(current,"")>=0)
result
=result+"b";
elseif(compare(current,"")>=0)
result
=result+"a";
}


returnresult;
}


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

publicStringgetFirstStr(Stringstr)...{
chara=str.charAt(0);
charaa[]=...{a};
Stringsss
=newString(aa);
if(Character.isDigit(aa[0]))
sss
="data";
elseif(a>='a'&&a<='z'||a>='A'&&a<='Z')
sss
="character";
else
sss
=getBeginCharacter(sss);
returnsss;
}


publicstaticvoidmain(String[]args)...{
System.out.println(
newGetCh2Spell().getBeginCharacter("你好吗!"));
//System.out.println("啊芭擦搭发啪然期击");
}

}

你可能感兴趣的:(F#,J#)