栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

day15 Java String类习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

day15 Java String类习题

文章目录
      • 1. 统计一个字符串中大写字母,小写字母,数字出现的次数
      • 2. 把一个字符串的首字母转成大写,其余的都是小写
      • 3. 把一个字符串的首字母转成大写,其余的都是小写
      • 4. 字符串翻转
      • 4. 统计大串中小串出现的次数


肯定的微笑
准备,往前出发




1. 统计一个字符串中大写字母,小写字母,数字出现的次数
        String s = "HadoopJava12138";
        int bigCount = 0;
        int smallCount = 0;
        int numberCount = 0;
        
        for(int i = 0; i < s.length(); i++){
            
            char c = s.charAt(i);
            
            if(c >= '0' && c <= '9'){
                numberCount++;
            }else if(c >= 'a' && c <= 'z'){
                smallCount++;
            }else if(c >= 'A' && c <= 'Z'){
                bigCount++;
            }
        }
        System.out.println("大写个数:"+bigCount);
        System.out.println("小写个数:"+smallCount);
        System.out.println("数字个数:"+numberCount);

2. 把一个字符串的首字母转成大写,其余的都是小写
String s = "HadoOPJava";
		//方式一
        String s1 = s.substring(0,1);
        String s2 = s.substring(1);

        s1 = s1.toUpperCase();
        s2 = s2.toLowerCase();

        String s3 = s1.concat(s2);
        //链式编程
        String s4 = s.substring(0,1)
                .toUpperCase()
                .concat(s.substring(1)
                        .toLowerCase());

        System.out.println(s3);
        System.out.println(s4);

		//方式二
		char c = s.charAt(0);
		//字符转大写方法
        c = Character.toUpperCase(c);
        String a = c + s.substring(1).toLowerCase();
        System.out.println(a);

3. 把一个字符串的首字母转成大写,其余的都是小写

把数组中的数据按照指定个格式拼接成一个字符串

		int[] arr = {1,2,3};
        String s = "[";

        for(int i = 0; i < arr.length; i++){
            if(i == arr.length-1){
                s += arr[i];
            }else {
                s += arr[i] + ",";
            }
        }
        s +="]";
        System.out.println(s);

4. 字符串翻转
 import java.util.Scanner;
 		
 		Scanner sc = new Scanner(System.in);
        String s = sc.next();
        
		String s1 ="";
        char[] c = s.toCharArray();
        for(int i = c.length-1; i >=0; i--){
            s1 += c[i];
        }
        System.out.println(s1);
4. 统计大串中小串出现的次数
		String s = "woaija vawozhenaija vawozhendeaija vawozhendehenaija ";
        String key = "123";

        int count = 0;
        while(true){
            int position = s.indexOf(key);
            if(position == -1){
                break;
            }
            s = s.substring(position + key.length());
            count++;
        }
        System.out.println(“出现次数为:”count);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号