package org.dromara.system.utils;

import lombok.extern.slf4j.XSlf4j;
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.exception.BadHanyuPinyinOutputFormatCombination;

import java.text.Collator;
import java.util.*;
import java.util.stream.*;


public class hanziUtils {


    /***
     * 传入字符串的首个字符获取首字母
     * @param chinese
     * @return
     */
    public static String getFirstLetter(String chinese) {
        StringBuilder initials = new StringBuilder();
        char initial = chinese.charAt(0);
        // 获取汉字的拼音
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(initial);
        if (pinyinArray != null) {  // 汉字
            initials.append(pinyinArray[0].charAt(0)); // 获取拼音的首字母
        } else {  // 非汉字
            initials.append(initial); // 直接附加该字符
        }

        return initials.toString().toUpperCase(); // 返回大写首字母
    }

    public static void main(String[] args) {
        String name = "张三";
        String initial = getFirstLetter(name);
        System.out.println("名字：" + name + " 的首字母是：" + initial);
    }


}
