Sometimes, there may be requirements like this:

 

For this effect, we can have a solution like this:

package bys.utils; import java.io.UnsupportedEncodingException; /** * Created by toutou on 2014/2/21 */ public class ChineseCharacterHelper { static final int GB_SP_DIFF = 160; Static final int[] secPosValueList = {1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5600}; Deposit / / national standard level of the starting location code corresponding to the pronunciation of the characters different pronunciation static final char [] firstLetter = {' a ', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y', 'z' }; @param characters * @return */ public static String getSpells(String characters) {public static String getSpells(String characters) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < characters.length(); i++) { char ch = characters.charAt(i); If ((ch >> 7) == 0) {buffer.append(ch); } else { char spell = getFirstLetter(ch); buffer.append(String.valueOf(spell)); } } return buffer.toString(); } public static Character getFirstLetter(char ch) {byte[] uniCode = null;} public static Character getFirstLetter(char ch) {byte[] uniCode = null; try { uniCode = String.valueOf(ch).getBytes("GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } if (uniCode[0] < 128 && uniCode[0] > 0) {// return null; } else { return convert(uniCode); }} /** * gets the first letter of a Chinese character. For example, the GB code of the Chinese character "you" is 0xC4/0xE3, and the GB code of the Chinese character "you" is 0x24/0x43 * 0x24. If the GB code of the Chinese character "you" is 160, then the GB code of the Chinese character "you" is 0x24/0x43 * 0x24. If the GB code of the Chinese character "you" is 160, then the GB code of the Chinese character "you" is 36. Private static char convert(byte[] bytes) {char result = '#'; private static char convert(byte[] bytes) {char result = '#'; int secPosValue = 0; int i; for (i = 0; i < bytes.length; i++) { bytes[i] -= GB_SP_DIFF; } secPosValue = bytes[0] * 100 + bytes[1]; for (i = 0; i < 23; i++) { if (secPosValue >= secPosValueList[i] && secPosValue < secPosValueList[i + 1]) { result = firstLetter[i]; break; } } return result; }}Copy the code

Invoke the sample String carName = ChineseCharacterHelper. GetSpells (String. The valueOf (user. The getName (). The charAt (0))). The toUpperCase ();

About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \