Java中根据中文汉字获取首字母

目录

1、引入依赖

2、代码实现

3、功能测试


1、引入依赖

        在Java中想要实现根据中文汉字获取首字母的功能有两种途径,分别是使用第三方库Pinyin4j和Java自带的RuleBasedCollator类实现,这里大概讲述关于第三方库Pinyin4j的使用方式;

        首先在项目中引入相关依赖:

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

2、代码实现

        创建PinYinUtil工具类,结合Pinyyin4j提供的方法来编写具体功能实现;



import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.stereotype.Component;

/**
 * @Author: ljh
 * @ClassName PinYinUtil
 * @Description TODO
 * @date 2023/4/27 17:19
 * @Version 1.0
 */
@Component
public class PinYinUtil {



    /**
     * @Author: ljh
     * @Description: 提取每个字符的首字母(大写)
     * @DateTime: 17:20 2023/4/27
     * @Params:
     * @Return
     */
    public static String getPinYinHeadChar(String str) {
        if (str == null || str.trim().equals("")) {
            return "";
        }
        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;
            }
        }
//        去除字符中包含的空格
//        convert = convert.replace(" ","");
//        字符转小写
//        convert.toLowerCase();
        return convert.toUpperCase();
    }


}

        上述功能代码中:getPinYinHeadChar() 方法就是根据字符获取首字母,其中主要是使用Pinyin4j中的 toHanguPinyinStringArray() 方法对单个字符提取首字母然后拼接结果,最后注释代码可以选择结果是否保留空格及转换字母大小写功能。

3、功能测试

结果保留空格并转大写:

结果去除空格并转小写:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这是一个简单的Java Swing实现字母拼写单词小游戏的代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SpellingGame extends JFrame implements ActionListener { // 单词列表 private String[] words = {"apple", "banana", "cherry", "orange", "pear"}; // 当前单词 private String currentWord; // 当前单词的字母列表 private char[] currentLetters; // 当前字母序号 private int currentLetterIndex; // 游戏界面的组件 private JLabel wordLabel; private JTextField letterTextField; private JButton startButton; private JButton nextButton; public SpellingGame() { setTitle("字母拼写单词小游戏"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 150); setLocationRelativeTo(null); // 布局 setLayout(new GridLayout(3, 2)); // 单词标签 wordLabel = new JLabel("", JLabel.CENTER); add(wordLabel); // 字母输入框 letterTextField = new JTextField(10); letterTextField.setEnabled(false); add(letterTextField); // 开始按钮 startButton = new JButton("开始"); startButton.addActionListener(this); add(startButton); // 下一个字母按钮 nextButton = new JButton("下一个字母"); nextButton.setEnabled(false); nextButton.addActionListener(this); add(nextButton); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { // 开始游戏 currentWord = words[(int) (Math.random() * words.length)]; currentLetters = currentWord.toCharArray(); currentLetterIndex = 0; wordLabel.setText(currentWord.charAt(0) + ""); letterTextField.setEnabled(true); letterTextField.setText(""); startButton.setEnabled(false); nextButton.setEnabled(true); } else if (e.getSource() == nextButton) { // 检查当前字母是否正确 char currentLetter = currentLetters[currentLetterIndex]; char inputLetter = letterTextField.getText().toLowerCase().charAt(0); if (currentLetter == inputLetter) { // 字母正确,显示下一个字母 currentLetterIndex++; if (currentLetterIndex < currentLetters.length) { wordLabel.setText(wordLabel.getText() + currentLetters[currentLetterIndex]); letterTextField.setText(""); } else { // 所有字母都拼写正确,显示恭喜信息 JOptionPane.showMessageDialog(this, "恭喜你,拼写正确!", "恭喜", JOptionPane.INFORMATION_MESSAGE); startButton.setEnabled(true); nextButton.setEnabled(false); letterTextField.setEnabled(false); } } else { // 字母错误,弹出提示框 JOptionPane.showMessageDialog(this, "字母拼写错误!", "错误", JOptionPane.ERROR_MESSAGE); } } } public static void main(String[] args) { new SpellingGame(); } } ``` 该代码实现了一个简单的字母拼写单词小游戏,点击开始按钮后随机选择一个单词,只显示第一个字母,然后用户逐个输入其他字母,如果输入正确,就显示下一个字母,如果输入错误,就弹出提示框。当所有字母都拼写正确时,显示恭喜信息。玩家可以选择重新开始游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想养一只!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值