字符串练习

1.、随便输入一段数字字符串,把其中连续出现次数最多的字符找出来并统计出连续出现的字符此次数, 如:233111333。
 

public class Zifuchuan {
    public static void main(String[]args){
        char ch=' ';
        String str="2331113333";
        int times=0;
        int time=1;
        for(int i=0;i<str.length()-1;i++){
            if(str.charAt(i)==str.charAt(i+1)) time++;
            else {
                if(time>times){
                    times=time;
                    ch=str.charAt(i);
                }time=1;
            }
        }
        if(time>times){
            times=time;
            ch=str.charAt(str.length()-1);
        }
        System.out.println(ch+" "+times);
    }
}

2.、有如下字符串“iu7i8csr83sdf9",将其中的数字摘取出来组成一个 int 的数值输出。
 

public class ZI {
    public static void main(String[] args) {
        String s = "iu7i8csr83sdf9";
        char[] c = s.toCharArray();
        int[] is = new int[c.length];  //定义int类型数组存储数字
        int count = 0;
        for (char c1 : c) {
            if (c1 >= 48 && c1 <= 58) {
                String s1 = String.valueOf(c1);  //将字符转为字符串
                is[count] = Integer.parseInt(s1);  //将字符串转为int数字
                count++;
            }
        }
        for (int i = 0; i < count; i++) {
            System.out.print(is[i]);
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值