You can use pinyin4j. jar to convert Chinese characters to pinyin. The Code is as follows:
[Java]
View plaincopyprint?
- Package mainapp;
- 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 testpinyin4j
- {
- /**
- * Convert Chinese characters to full spelling
- *
- * @ Param SRC
- * @ Return string
- */
- Public static string getpinyin (string SRC)
- {
- Char [] T1 = NULL;
- T1 = SRC. tochararray ();
- String [] T2 = new string [t1.length];
- // Set the output format of Chinese characters and Pinyin
- 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 can be a Chinese character
- // System. Out. println (T1 [I]);
- If (character. tostring (T1 [I]). Matches ("[// u4e00-// u9fa5] + "))
- {
- T2 = pinyinhelper. tohanyupinyinstringarray (T1 [I], T3); // save all the Chinese characters to the T2 array.
- T4 + = t2 [0]; // obtain the first pronunciation of the Chinese character and connect it to the string T4.
- }
- Else
- {
- // If it is not a Chinese character, indirectly retrieve the character and connect it to the string T4
- T4 + = character. tostring (T1 [I]);
- }
- }
- }
- Catch (badhanyupinyinoutputformatcombination E)
- {
- E. printstacktrace ();
- }
- Return T4;
- }
- /**
- * Extract the first letter of each Chinese Character
- *
- * @ Param Str
- * @ Return string
- */
- Public static string getpinyinheadchar (string Str)
- {
- String convert = "";
- For (Int J = 0; j <Str. Length (); j ++)
- {
- Char word = Str. charat (j );
- // Extract the first letter of a Chinese character
- String [] pinyinarray = pinyinhelper. tohanyupinyinstringarray (Word );
- If (pinyinarray! = NULL)
- {
- Convert + = pinyinarray [0]. charat (0 );
- }
- Else
- {
- Convert + = word;
- }
- }
- Return convert;
- }
- /**
- * Convert a string to an ascii code.
- *
- * @ Param cnstr
- * @ Return string
- */
- Public static string getcnascii (string cnstr)
- {
- Stringbuffer strbuf = new stringbuffer ();
- // Convert a string to a byte sequence
- Byte [] bgbk = cnstr. getbytes ();
- For (INT I = 0; I <bgbk. length; I ++)
- {
- // Convert each character to an ascii code
- Strbuf. append (integer. tohexstring (bgbk [I] & 0xff ));
- }
- Return strbuf. tostring ();
- }
- Public static void main (string [] ARGs)
- {
- String cnstr = "Chen ";
- System. Out. println (getpinyin (cnstr ));
- System. Out. println (getpinyinheadchar (cnstr ));
- System. Out. println (getcnascii (cnstr ));
- }
- }
You can search for "pinyin4j. Jar" from the "resource channel" of csdn to find related resources and introduce them to the project.
Pinyin4j can obtain pinyin corresponding to Chinese characters, and its format can be customized.
Features:
1. Support for multiple pinyin systems:
Chinese pinyin
General pinyin
Weishi pinyin
Phonetic transcription
Yali
Mandarin and Rome
2. Support for polyphonic words
The correct pinyin can be obtained based on the context.
3. Support for multiple pinyin formats
Case sensitivity
Supports Unicode U, V, and u
Supports digital tones (lü3), tone symbols (lüs), and non-tone marks (lüs)
From: http://blog.csdn.net/ziwen00/article/details/5436123
You can use pinyin4j. jar to convert Chinese characters to pinyin. The Code is as follows:
[Java]
View plaincopyprint?
- Package mainapp;
- 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 testpinyin4j
- {
- /**
- * Convert Chinese characters to full spelling
- *
- * @ Param SRC
- * @ Return string
- */
- Public static string getpinyin (string SRC)
- {
- Char [] T1 = NULL;
- T1 = SRC. tochararray ();
- String [] T2 = new string [t1.length];
- // Set the output format of Chinese characters and Pinyin
- 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 can be a Chinese character
- // System. Out. println (T1 [I]);
- If (character. tostring (T1 [I]). Matches ("[// u4e00-// u9fa5] + "))
- {
- T2 = pinyinhelper. tohanyupinyinstringarray (T1 [I], T3); // save all the Chinese characters to the T2 array.
- T4 + = t2 [0]; // obtain the first pronunciation of the Chinese character and connect it to the string T4.
- }
- Else
- {
- // If it is not a Chinese character, indirectly retrieve the character and connect it to the string T4
- T4 + = character. tostring (T1 [I]);
- }
- }
- }
- Catch (badhanyupinyinoutputformatcombination E)
- {
- E. printstacktrace ();
- }
- Return T4;
- }
- /**
- * Extract the first letter of each Chinese Character
- *
- * @ Param Str
- * @ Return string
- */
- Public static string getpinyinheadchar (string Str)
- {
- String convert = "";
- For (Int J = 0; j <Str. Length (); j ++)
- {
- Char word = Str. charat (j );
- // Extract the first letter of a Chinese character
- String [] pinyinarray = pinyinhelper. tohanyupinyinstringarray (Word );
- If (pinyinarray! = NULL)
- {
- Convert + = pinyinarray [0]. charat (0 );
- }
- Else
- {
- Convert + = word;
- }
- }
- Return convert;
- }
- /**
- * Convert a string to an ascii code.
- *
- * @ Param cnstr
- * @ Return string
- */
- Public static string getcnascii (string cnstr)
- {
- Stringbuffer strbuf = new stringbuffer ();
- // Convert a string to a byte sequence
- Byte [] bgbk = cnstr. getbytes ();
- For (INT I = 0; I <bgbk. length; I ++)
- {
- // Convert each character to an ascii code
- Strbuf. append (integer. tohexstring (bgbk [I] & 0xff ));
- }
- Return strbuf. tostring ();
- }
- Public static void main (string [] ARGs)
- {
- String cnstr = "Chen ";
- System. Out. println (getpinyin (cnstr ));
- System. Out. println (getpinyinheadchar (cnstr ));
- System. Out. println (getcnascii (cnstr ));
- }
- }
You can search for "pinyin4j. Jar" from the "resource channel" of csdn to find related resources and introduce them to the project.
Pinyin4j can obtain pinyin corresponding to Chinese characters, and its format can be customized.
Features:
1. Support for multiple pinyin systems:
Chinese pinyin
General pinyin
Weishi pinyin
Phonetic transcription
Yali
Mandarin and Rome
2. Support for polyphonic words
The correct pinyin can be obtained based on the context.
3. Support for multiple pinyin formats
Case sensitivity
Supports Unicode U, V, and u
Supports digital tones (lü3), tone symbols (lüs), and non-tone marks (lüs)
From: http://blog.csdn.net/ziwen00/article/details/5436123