sequence

A mature product, the search function of the home page in addition to the normal keyword matching; Also consider that the user forgot to enter Chinese characters or that the user wants to search by the first letter of a keyword.

Amiao company is making a game box recently, which contains many games. One requirement is that users can search the game according to the initials of the game name, for example, ZWDZJS can be searched to find plants vs. zombies, etc. Input HZW can search out the king of one Piece.

How is the function implemented? Next, Amiao will directly take you to practice.


1. Import dependency packages

    <dependency>
  <groupId>com.belerweb</groupId>
  <artifactId>pinyin4j</artifactId>
  <version>2.5.1</version>
 </dependency>
Copy the code

2, to achieve pinyin tool class

2.1. Obtain full spelling according to Chinese characters

 public String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
 LOWERCASE UPPERCASE(LOWERCASE)  t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  // Set the tone  t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   t3.setVCharType(HanyuPinyinVCharType.WITH_V);  String t4 = "";  int t0 = t1.length;  try {  for (int i = 0; i < t0; i++) {  // Check 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]);  }  }  return t4;  } catch (BadHanyuPinyinOutputFormatCombination e1) {  e1.printStackTrace();  }  return t4;  } Copy the code

2.2. Get the first letter based on Chinese characters

public 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;  } Copy the code

3, test,

Test code:

public static void main(String[] args) {
  PinYinUtil pinYinUtil = new PinYinUtil();
  String str = Plants vs. Zombies.;
  String quanpin=pinYinUtil.getPingYin(str);
  String head=pinYinUtil.getPinYinHeadChar(str);
 System.out.println("Chinese Name :"+str);  System.out.println("The whole put together."+quanpin);  System.out.println("Initial :"+head);  } Copy the code

Test results:

English name: Plants vs. ZombiesWhole put together: zhiwudazhanjiangshiInitials: ZWDZJSCopy the code

Well, that’s enough for today’s sharing, and we’ll continue tomorrow.

I am Amiao, and your [Sanlian] is the biggest motivation for amiao’s creation. If there are any mistakes or suggestions in this blog, please leave a comment!

The article is constantly updated, you can search “The way of Java learning” on wechat to read it in the first time, reply [666] I prepared the necessary e-book programmer + more than hd teaching videos, as well as surprise interview questions, welcome to pick up.