String有关算法

身份证号判断周岁

public Integer checkExpertAge(String idCard) {
    //  1.  获取身份证中的出生年、月、日
    Integer personYear = Integer.parseInt(idCard.substring(6, 10));
    Integer personMonth = Integer.parseInt(idCard.substring(10, 12));
    Integer personDay = Integer.parseInt(idCard.substring(12, 14));
    //  2.  获取当前时间的年、月、日
    Calendar cal = Calendar.getInstance();
    Integer yearNow = cal.get(Calendar.YEAR);
    Integer monthNow = cal.get(Calendar.MONTH) + 1;
    Integer dayNow = cal.get(Calendar.DATE);
    //  3.  用当前年月日减去生日年月日
    Integer yearMinus = yearNow - personYear;
    Integer monthMinus = monthNow - personMonth;
    Integer dayMinus = dayNow - personDay;
    //  4.  周岁判断
    Integer age = yearMinus;
    if (yearMinus == 0) {
        //  出生年份为当前年份
        age = 0;
    } else {
        //  出生年份大于当前年份
        if (monthMinus < 0) {
            //  出生月份小于当前月份时,还没满周岁
            age = age - 1;
        }
        if (monthMinus == 0) {
            //  当前月份为出生月份时,判断日期
            if (dayMinus < 0) {
                //  出生日期小于当前月份时,没满周岁
                age = age - 1;
            }
        }
    }return age;
}

用四种方法统计下面字符串"葡萄"出现的次数

public class Test01{
    public static void main(String[] args){
        String str = "吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮";
        //请用四种方法统计 葡萄 出现的次数
        //第一种
        String str1 = str.replace("葡萄"," ");
        char[] data = str1.toCharArray();
        int count = 0;
        for(char a : data){
            if(a == ' '){
                count++;
            }
        }
        System.out.println(count);

        //第二种
        int count = 0;
        int x = 0;

        while(x != -1){
            x = str.indexOf("葡萄");
            if(x != -1){
                count++;
                str = str.replaceFirst("葡萄","");
            }
        }
        System.out.println(count);
        
        //第三种
        for(int x = 0; x < str.length() ; x++){
            if(str.contains("葡萄")){
                str = str.replaceFirst("葡萄","");
            }else{
                System.out.println(x);
                break;
            }
        }
        
        //第四种
        String[] data = str.split("葡萄");

        if(!str.endsWith("葡萄")){    //这里startsWith("葡萄")在split时,会在开头产生空串
            System.out.println(data.length -1);
        }else{
            System.out.println(data.length);
        }
        
        //第五种
        int count = 0;
        int x = 0;
        while(x != -1){
            x = str.indexOf("葡萄");
            if(x != -1){
                if((x+2) < str.length()){
                    str = str.substring(x+2);
                }
                count++;
            }
        }
        System.out.println(count);
        
        //第六种
        String str2 = str.replace("葡萄","");
        System.out.println((str.length() - str2.length())/2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值