源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Java简单统计字符串中汉字,英文字母及数字数量的方法

  • 时间:2020-06-23 19:29 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Java简单统计字符串中汉字,英文字母及数字数量的方法
本文实例讲述了Java简单统计字符串中汉字,英文字母及数字数量的方法。分享给大家供大家参考,具体如下:
package org.zhy.demo.algorithm;
/**
 * 有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数
 *
 * @author Administrator
 *
 */
public class Str {
  public static void main(String[] args) {
    String str = "adasfAAADFD阿萨德发123";
    int unicodeCount = 0;
    int szCount = 0;
    int zmCount = 0;
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if (c >= '0' && c <= '9') {
        szCount++;
      }else if((c >= 'a' && c<='z') || (c >= 'A' && c<='Z')){
        zmCount++;
      }else{
        unicodeCount++;
      }
    }
    System.out.println(unicodeCount);
    System.out.println(szCount);
    System.out.println(zmCount);
  }
}

[b]PS:这里再为大家推荐2款非常方便的统计工具供大家参考使用:[/b] [b]在线字数统计工具: [/b][url=http://tools.jb51.net/code/zishutongji]http://tools.jb51.net/code/zishutongji[/url] [b]在线字符统计与编辑工具: [/b][url=http://tools.jb51.net/code/char_tongji]http://tools.jb51.net/code/char_tongji[/url] 更多关于java算法相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/632.htm]Java数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/830.htm]Java操作DOM节点技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/687.htm]Java文件与目录操作技巧汇总[/url]》和《[url=http://www.1sucai.cn/Special/682.htm]Java缓存操作技巧汇总[/url]》 希望本文所述对大家java程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部