Package test;
Import java. Io. unsupportedencodingexception;
Import net. SourceForge. pinyin4j. pinyinhelper;
/***/
/**
* Obtain the first letter of a given Chinese character string, that is, the initials.
* <P> title: chinesechartoen </P>
* <P> @ author javer QQ: 84831612 </P>
*
* @ Version 1.0
* @ Date 2004-02-19
* Note: Only Chinese characters in the gb2312 character set are supported.
*/
Public final class chinesechartoen {
Private Final Static int [] li_secposvalue =
{
1601,163 7, 1833,207 8, 2274,230 2, 2433,259 4, 2787,310 6, 3212,347 2,
3635,372 2, 3730,385 8, 4027,408 6, 4390,455 8, 4684,492 5, 5249,559 0
};
Private Final Static string [] lc_firstletter =
{
"A", "B", "C", "D", "E", "F", "g", "H", "J", "K ", "L", "M", "n", "O", "P ",
"Q", "r", "S", "T", "W", "X", "Y", "Z"
};
/***/
/**
* Obtain the first letter of a given Chinese character string, that is, the initials.
*
* @ Param STR specifies a Chinese character string
* @ Return the initials
*/
Public String getallfirstletter (string Str ){
If (STR = NULL | Str. Trim (). Length () = 0 ){
Return "";
}
String _ STR = "";
For (INT I = 0; I <Str. Length (); I ++ ){
_ STR = _ STR + this. getfirstletter (Str. substring (I, I + 1 ));
}
System. Out. println (Str. Length ());
Return _ STR;
}
/***/
/**
* Get the first letter of a given Chinese character, that is, the initials.
*
* @ Param Chinese Characters
* @ Return specifies the initials of a Chinese character.
*/
Public String getfirstletter (string Chinese ){
If (Chinese = NULL | Chinese. Trim (). Length () = 0 ){
Return "";
}
Chinese = This. conversionstr (Chinese, "GBK", "ISO8859-1 ");
If (Chinese. Length ()> 1) // judge whether it is a Chinese character
{
Int li_sectorcode = (INT) Chinese. charat (0); // Chinese Character area code
Int li_positioncode = (INT) Chinese. charat (1); // Chinese character location code
Li_sectorcode = li_sectorcode-160;
Li_positioncode = li_positioncode-160;
Int li_secposcode = li_sectorcode * 100 + li_positioncode; // Chinese character location code
If (li_secposcode> 1600 & li_secposcode <5590 ){
For (INT I = 0; I <23; I ++ ){
If (li_secposcode> = li_secposvalue [I] &
Li_secposcode <li_secposvalue [I + 1]) {
Chinese = lc_firstletter [I];
Break;
}
}
} Else // non-Chinese characters, symbols or ASCII codes
{
Chinese = This. conversionstr (Chinese, "ISO8859-1", "GBK ");
Chinese = Chinese. substring (0, 1 );
}
}
Return Chinese;
}
Public String getallfirstletters (string Str ){
String convert = "";
For (Int J = 0; j <Str. Length (); j ++ ){
Char word = Str. charat (j );
String [] pinyinarray = pinyinhelper. tohanyupinyinstringarray (Word );
If (pinyinarray! = NULL ){
Convert + = pinyinarray [0]. charat (0 );
} Else {
Convert + = word;
}
}
Return convert;
}
/***/
/**
* String encoding and conversion
*
* @ Param STR the string to be converted into Encoding
* @ Param charsetname original Encoding
* @ Param tocharsetname: encoding after conversion
* @ Return the encoded string
*/
Private string conversionstr (string STR, string charsetname, string tocharsetname ){
Try {
STR = new string (Str. getbytes (charsetname), tocharsetname );
}
Catch (unsupportedencodingexception ex ){
System. Out. println ("string encoding conversion exception:" + ex. getmessage ());
}
Return STR;
}
Public static void main (string [] ARGs ){
Chinesechartoen CTE = new chinesechartoen ();
System. Out. println ("the first letter of 'sleep 'is:" + CTE. getfirstletter ("Sleep "));
System. Out. println ("the first pinyin Letter of 'javer sleep 'is:" + CTE. getfirstletter ("javer sleep "));
System. Out. println ("dummies in Yunlong" + CTE. getallfirstletters (" "));
}
}
Method 2:
Package test;
Import java. Io. unsupportedencodingexception;
Import net. SourceForge. pinyin4j. pinyinhelper;
Import net. SourceForge. pinyin4j. format. hanyupinyincasetype;
Import net. SourceForge. pinyin4j. format. hanyupinyinoutputformat;
Import net. SourceForge. pinyin4j. format. hanyupinyintonetype;
Import net. SourceForge. pinyin4j. format. hanyupinyinvchartype;
Import net. SourceForge. pinyin4j. format. Exception. badhanyupinyinoutputformatcombination;
Public class cntopy {
// Convert Chinese characters to full spelling
Public static string getpingyin (string SRC ){
Char [] T1 = NULL;
T1 = SRC. tochararray ();
String [] T2 = new string [t1.length];
Hanyupinyinoutputformat T3 = new hanyupinyinoutputformat ();
T3.setcasetype (hanyupinyincasetype. lowercase );
T3.settonetype (hanyupinyintonetype. without_tone );
T3.setvchartype (hanyupinyinvchartype. with_v );
String t4 = "";
Int T0 = t1.length;
Try {
For (INT I = 0; I <t0; I ++)
{
// Determine whether it is a Chinese character
If (Java. Lang. character. tostring (T1 [I]). Matches ("[// u4e00-// u9fa5] + "))
{
T2 = pinyinhelper. tohanyupinyinstringarray (T1 [I], T3 );
T4 + = t2 [0];
}
Else
T4 + = java. Lang. character. tostring (T1 [I]);
}
// System. Out. println (T4 );
Return T4;
}
Catch (badhanyupinyinoutputformatcombination E1 ){
E1.printstacktrace ();
}
Return T4;
}
// Returns the first letter of a Chinese character.
Public static string getpinyinheadchar (string Str ){
String convert = "";
For (Int J = 0; j <Str. Length (); j ++ ){
Char word = Str. charat (j );
String [] pinyinarray = pinyinhelper. tohanyupinyinstringarray (Word );
If (pinyinarray! = NULL ){
Convert + = pinyinarray [0]. charat (0 );
} Else {
Convert + = word;
}
}
Return convert;
}
// Transfer the string to an ascii code
Public static string getcnascii (string cnstr)
{
Stringbuffer strbuf = new stringbuffer ();
Byte [] bgbk = cnstr. getbytes ();
For (INT I = 0; I <bgbk. length; I ++ ){
// System. Out. println (integer. tohexstring (bgbk [I] & 0xff ));
Strbuf. append (integer. tohexstring (bgbk [I] & 0xff ));
}
Return strbuf. tostring ();
}
Public static void main (string [] ARGs ){
String cnstr = "People's Republic of China ";
System. Out. println (getpingyin (cnstr ));
System. Out. println (getpinyinheadchar (cnstr ));
}
}