关于统计字符串中子串,字符串中每种类型(数字,字母,中文,空格),字符串中每个字符出现的个数

关于统计字符串中子串,字符串中每种类型(数字,字母,中文,空格),字符串中每个字符出现的个数

1.统计字符串中子串

关于StringTokenizer切割这个方法无法实现,留下为了给自己提个醒

/*
统计小字符串在大字符串中出现的次数;
 */
public class QueryStr1 {
    public static void main(String[] args) {
        String bigString = "dsadasdadasd";
        String smallString = "sd";
        System.out.println(getStrByIndex(bigString,smallString));
        System.out.println(getStrByRepalace(bigString,smallString));
        System.out.println(getStrByStringTokenizer(bigString,smallString));
        StringTokenizer tokenizer = new StringTokenizer("dfdsfsf","sf");
        while (tokenizer.hasMoreTokens()){
            System.out.println(tokenizer.nextToken());
        }
    }

    public static int getStrByIndex(String big, String small) {
        int cou = 0;
        int sta = 0;
        while ((sta = big.indexOf(small, sta)) != -1) {
            sta += small.length();
            cou++;
        }

        return cou;
    }

    public static int getStrByStringTokenizer(String big, String small) {
        StringTokenizer tokenizer = new StringTokenizer(big,small,true);
        int cou = 0;
        //System.out.println(tokenizer);
        String s="";
        while (tokenizer.hasMoreTokens()) {
            if (small.equals(s=tokenizer.nextToken())) {

                cou++;
            }
            System.out.println(s);
        }
        return cou;
    }

    public static int getStrByRepalace(String big, String small) {
        return (big.length()-big.replace(small,"").length())/small.length();
    }
}

2. 统计每个字符在字符串中出现的字数

/*
统计字符串中每种字符出现的个数
 */
public class QuerySt2 {
    public static void main(String[] args) {
        String str = "sdsad进口红酒的sds642";
        HashMap<Character, Integer> map = new HashMap<>();
        charCount(map,str);
        System.out.println(map.toString());
    }

    public static void charCount(HashMap<Character, Integer> hashMap, String str) {
        char[] chars = str.toCharArray();
        for (char c : chars){
            if (hashMap.containsKey(c)){
                int num = hashMap.get(c)+1;
                hashMap.put(c,num);
            }else {
                hashMap.put(c,1);
            }
        }
    }
}

3.统计每种字符类型在字符串中出现的次数

/*
统计字符串中字母,数字,汉字的个数
 */
public class QuerStr3 {
    public static void main(String[] args) {

        show("sadasdasd手机打开的768687sd");
    }

    public static void show(String str) {
        int letterCount = 0;
        int digitCount = 0;
        int chineseCount = 0;
        int spaceCount = 0;
        int otherCount = 0;
        String le = "[a-zA-Z]";
        String di = "[0-9]";
        String ch = "[\u4e00-\u9fa5]";
        String sp = "\\s";

        char [] chars = str.toCharArray();
        String [] strings = new String[chars.length];
        for (int i =0 ; i<chars.length; i++){
            strings[i] = String.valueOf(chars[i]);
        }

        for (String s : strings){
            if (s.matches(le)){
                letterCount++;
            }
            if (s.matches(di)){
                digitCount++;
            }
            if (s.matches(ch)){
                chineseCount++;
            }
            if (s.matches(sp)){
                spaceCount++;
            }
        }
        System.out.println("字母"+letterCount);
        System.out.println("数字"+digitCount);
        System.out.println("中文"+chineseCount);
        System.out.println("空格"+spaceCount);
    }
}

如果有问题还请大家指出

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值