统计字符串中汉字的个数
字符串可以包括数字、字母、汉字或者其他字符。使用Charater类的isDigit()方法可以判断字符串中的某个字符是否为数字, 使用Character类的isLetter()方法可以判断字符串中的某个字符是否为字母。 本案例将介绍用“正则表达式”来判断字符串中的某个字符是否为汉字,并统计该字符串中汉字的数量。 关键技术: Java中提供Pattern用于正则表达式的编译方式,该类的静态方法matches()可以执行正则表达式的匹配。该方法的声明如下: public static boolean matches(String regex,CharSequence) 该方法中,如果CharSequence与regex匹配,则返回true,否则返回false,其中regex表示“正则表达式”,CharSequence表示原字符串。 设计过程: 1)在项目中创建窗体类ChineseAmount。在窗体类中添加用户输入的文本域控件、显示汉字数量的文本框控件和计算汉字数量的“计算汉字”按钮。 2)编写“计算汉字”按钮的事件处理方法。在该方法中获取用户输入的字符串,然后,遍历字符串中的每一个字符,使用正则表达式判断字符是否为汉字, 再根据判断结果对汉字进行计数,最后把计数结果显示到界面文本框中。 关键代码如下: private void do_button_actionPerformed(ActionEvent e) { String text = chineseArea.getText();//获取用户输入 int amount = 0;//创建统计汉字的计数器 for (int i = 0; i < text.length(); i++) {//遍历字符串每一个字符 //使用正则表达式,判断字符是否为汉字编码 boolean matches = Pattern.matches("^[u4E00-u9FA5]{0,}$","" + text.charAt(i)); if (matches) {//如果是汉字 amount++;//则累加 } } numField.setText(amount + ""); } 完整代码如下: package cn.str.opera; //import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.regex.Pattern; public class ChineseAmount extends JFrame { private JPanel contentPane; private JTextArea chineseArea; private JTextField numField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ChineseAmount frame = new ChineseAmount(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ChineseAmount() { setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,450,300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5,5,5)); setContentPane(contentPane); contentPane.setLayout(null); chineseArea = new JTextArea(); chineseArea.setBounds(115,20,300,133); contentPane.add(chineseArea); JLabel lblNewLabel = new JLabel("u8F93u5165u4E00u6BB5u6587u5B57:"); lblNewLabel.setBounds(10,22,95,33); contentPane.add(lblNewLabel); JButton btnNewButton = new JButton("u8BA1u7B97u6C49u5B57"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { do_button_actionPerformed(e); } }); btnNewButton.setBounds(115,193,93,23); contentPane.add(btnNewButton); numField = new JTextField(); numField.setBounds(237,194,66,21); contentPane.add(numField); numField.setColumns(10); JLabel lblNewLabel_1 = new JLabel("u4E2Au6C49u5B57"); lblNewLabel_1.setBounds(325,197,15); contentPane.add(lblNewLabel_1); } private void do_button_actionPerformed(ActionEvent e) { String text = chineseArea.getText();//获取用户输入 int amount = 0;//创建统计汉字的计数器 for (int i = 0; i < text.length(); i++) {//遍历字符串每一个字符 //使用正则表达式,判断字符是否为汉字编码 boolean matches = Pattern.matches("^[u4E00-u9FA5]{0,"" + text.charAt(i)); if (matches) {//如果是汉字 amount++;//则累加 } } numField.setText(amount + ""); } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |