Need to use Pingyin4j.jar
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; public class Pingyin {/** * converts Chinese characters to full spell * * @param src * @return string/public static string Getpinyin (St
Ring src) {char[] T1 = null;
T1 = Src.tochararray ();
System.out.println (t1.length);
string[] t2 = new String[t1.length];
System.out.println (t2.length);
Set the format of Chinese pinyin output 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++) {//Judge whether it is Kanji character//System.out.println (T1[i]); if (character.tostring (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) {
T2 = Pinyinhelper.tohanyupinyinstringarray (t1[i], T3)//To save a few of the Chinese characters to the t2 array t4 + = t2[0];//Remove the first pronunciation of the Chinese spelling and connect to the string T4 after} else
{//If it is not a kanji character, remove the character directly and connect to the string t4 after T4 + = character.tostring (T1[i]);}
catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
return t4; /** * Extracts the first letter of each kanji * @param str * @return string/public static string Getpingyinheadchar (String str) {Strin
G convert = "";
for (int j = 0; J < Str.length (); j + +) {char word = Str.charat (j);
Extracts the first letter of Chinese characters string[] Pinyinarray = Pinyinhelper.tohanyupinyinstringarray (word, NULL);
if (Pinyinarray!= null) {convert + + pinyinarray[0].charat (0);
else {convert + + word;
} return to convert; /** * Converts a string to ASCII * @param cnstr * @return string/public static string Getcnascii (String cnstr) {StringBuffer
Strbuf = new StringBuffer ();
Converts a string to a byte sequence byte[] BGBK = Cnstr.getbytes (); for (int i = 0; i < bgbk.length i++) {//System.out.println (integer.tohexstring (bgbk[i) &amP
0xFF));
Converts each character to an ASCII code 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 (Getpinyin (cnstr));
System.out.println (Getcnascii (cnstr));
System.out.println (Getpingyinheadchar (cnstr)); }
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Find Chinese characters in a map by pinyin
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Scanner;
Import Java.util.Set;
Import Java.util.Map.Entry; public class Alphafindword {/** * @param args/@SuppressWarnings ("unchecked") public static void main (string[ ] args) {map<string, string> map=new hashmap<string,string> (); Map.put ("Open", "ZK"); Map.put ("Maniac", "ZK"); Map
. Put ("Andy Lau", "LDH");
Map.put ("Jacky Cheung", "Zxy");
Map.put ("Leehom Wang", "WLH");
Map.put ("Deng Zi qi", "dzq");
Map.put ("Comfortable", "SC");
Map<string,list> map2=new hashmap<string,list> ();
Set Set=map.entryset ();
Iterator It=set.iterator ();
while (It.hasnext ()) {map.entry<string,string> entry= (entry<string, string>) It.next ();
List Lst=map2.get (Entry.getvalue ());
if (lst==null) {lst=new ArrayList (); Map2.put (Entry.getvalue (), LST);} lst.add (Entry.getkey ());
} while (true) {Scanner scanner=new Scanner (system.in); String Str=scanner.nextline ();
SYSTEM.OUT.PRINTLN (Map2.get (str));
if (Str.equals ("Exit")) {break;}} }
}
Author: csdn Blog u010026901