当前位置: 代码迷 >> Java相关 >> 如何在字符串中统计汉字、字母、符号的个数
  详细解决方案

如何在字符串中统计汉字、字母、符号的个数

热度:295   发布时间:2010-04-10 23:50:42.0
如何在字符串中统计汉字、字母、符号的个数
谢谢
搜索更多相关的解决方案: 字母  统计  符号  字符  汉字  

----------------解决方案--------------------------------------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CountTest {
    public static void main(String args[]) {
        String str = "asdvdfdDERE123ABCD0012587我一二三四五";
        System.out.println("Numbers:" + countNumber(str));
        System.out.println("Letters:" + countLetter(str));
        System.out.println("Chinese:" + countChinese(str));
    }

    public static int countNumber(String str) {
        int count = 0;
        Pattern p = Pattern.compile("\\d");
        Matcher m = p.matcher(str);
        while(m.find()){
            count++;
        }
        return count;
    }

    public static int countLetter(String str) {
        int count = 0;
        Pattern p = Pattern.compile("[a-zA-Z]");
        Matcher m = p.matcher(str);
        while(m.find()){
            count++;
        }
        return count;
    }

    public static int countChinese(String str) {
        int count = 0;
        Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = p.matcher(str);
        while(m.find()){
            count++;
        }
        return count;
    }

}

[ 本帖最后由 lampeter123 于 2010-4-12 11:19 编辑 ]
----------------解决方案--------------------------------------------------------
package chapter.denglu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Dendlu extends JFrame implements ActionListener
{
    //创建JPanel对象
   
    private JPanel jp = new JPanel();
   
    //创建标签数组
   
    private JLabel[] jlArray = { new JLabel("用户名"), new JLabel("密   码"), new JLabel("")};
   
    //创建按钮数组
   
    private JButton[] jbArray = { new JButton("登陆"), new JButton("清空"), new JButton("注册")};
   
    //创建文本框以及密码框
   
    private JTextField jtxtName = new JTextField();
    private JPasswordField jtxtPassword = new JPasswordField();
    public Dendlu()
    {
        //设置JPanel的布局管理器
        
        jp.setLayout(null);
        
        //对标签与按钮控件循环进行处理
        
        for (int i = 0; i < 2; i++)
        {
            //设置标签与按钮的大小位置
            
            jlArray[i].setBounds(30, 20 + i * 50, 80, 26);
            //jbArray[i].setBounds(50 + i * 110, 130, 80, 26);
            
            //将标签与按钮添加到JPanel容器中
            
            jp.add(jlArray[i]);
            //jp.add(jbArray[i]);
            
            
            
            
        }
        
        //对按钮控件循环进行处理
        
        for (int j = 0; j < 3; j++)
        {
            //设置按钮的大小位置
            
            if (j == 2)
            {
                System.out.println();
            }
            jbArray[j].setBounds(50 + j * 110, 130, 80, 26);
            
            //将按钮添加到JPanmel容器中
            
            jp.add(jbArray[j]);
            
            //为按钮注册动作事件监听器
            
            jbArray[j].addActionListener(this);
        }
        
        //设置文本框的大小位置
        
        jtxtName.setBounds(80, 20, 180, 30);
        
        //将文本框添加进JPanel容器
        
        jp.add(jtxtName);
        
        //为文本框注册动作事件监听器
        
        jtxtName.addActionListener(this);
        
        //设置密码框的大小位置
        
        jtxtPassword.setBounds(80, 70, 180, 30);
        
        //将密码框添加进JPanel容器
        
        jp.add(jtxtPassword);
        
        //设置密码框的回显字符
        
        jtxtPassword.setEchoChar('*');
        
        //为密码框注册动作事件监听器
        
        jtxtPassword.addActionListener(this);
        
        //设置用于显示登录状态的标签大小位置,并将其添加进JPanel容器
        
        jlArray[2].setBounds(10, 180, 300, 30);
        jp.add(jlArray[2]);
        
        //将JPanel容器添加进窗体
        
        this.add(jp);
        
        //设置窗体的标题、大小位置以及可见性
        
        this.setTitle("登录");
        this.setResizable(false);
        this.setBounds(100, 100, 400, 250);
        this.setVisible(true);
    }
    //实现ActionListener接口中的方法
   
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == jtxtName)
        {
            //事件源为文本框
            //切换输入焦点到密码框
            
            jtxtPassword.requestFocus();
        }
        else if (e.getSource() == jbArray[1])
        {
            //事件源为清空按钮
            //清空所有信息
            
            jlArray[2].setText("");
            jtxtName.setText("");
            jtxtPassword.setText("");
            
            //将输入焦点设置到文本框
            
            jtxtName.requestFocus();
            
        }
        else
        {
            String str1[] = {"a", "b", "c", "d","e"};
            String str2[] = {"1", "2", "3", "4","5"};
            //事件源为登录按钮
            //判断用户名和密码是否匹配
            int l = str1.length;
            int m = str2.length;
            String M=jtxtName.getText();
            String N=String.valueOf(jtxtPassword.getPassword());
            for (int k = 0; k < l; k++)
                for (int q = 0; q < m; q++)
            {
            if (M.equals(str1[k]) && N.equals(str2[q]))
            {
                //登录成功
               
                jlArray[2].setText("欢迎登陆!!!");
               
            }
            else
            {
                //登录失败
               
                jlArray[2].setText("你的用户名或密码出错!!!");
               
            }
            }
        }
    }
    public static void main(String[] args)
    {
        //创建Dendlu窗体对象
        
        new Dendlu();
    }
}

为什么输入a和1不能登录,而输入e和5就能登录呢

[ 本帖最后由 hmq16 于 2010-4-20 19:41 编辑 ]
----------------解决方案--------------------------------------------------------
  相关解决方案