首页 > 编程语言 > 详细

JAVA-String类的应用

时间:2020-07-16 18:20:07      阅读:45      评论:0      收藏:0      [点我收藏+]

题目一:获取指定字符串中,大写字母、小写字母、数字的个数。

String str1 = "*Aab1B cd/e2f";
        int bigCount = 0;
        int smallCount = 0;
        int numberCount = 0;
        char[] array = str1.toCharArray();
        for (int i = 0; i < array.length; i++)
        {
            if (array[i] >= 65 && array[i] <= 90)// 判断大写
            // if (ch>=‘A‘ && ch<=‘Z‘) {这样也行的
            {
                bigCount++;
            } else if (array[i] >= 97 && array[i] <= 122)// 判断小写
            // } else if (ch>=‘a‘ && ch<=‘z‘) {
            {
                smallCount++;
            } else if (array[i] >= 48 && array[i] <= 57)// 判断数字
            // } else if (ch>=‘0‘ && ch<=‘9‘) {
            {
                numberCount++;
            }
        }
        System.out.println(bigCount);
        System.out.println(smallCount);
        System.out.println(numberCount);
    }

题目二: 字符串的第一个大写字符变为小写,后面的字符变为大写。

public class Person {
    public static void main(String[] args) {
        // 字符串的第一个大写字符变为小写,后面的字符变为大写
        changeLetter("*fATzb1B cd/e2f");
        changeLetter("*DETzb1B cd/e2f");
        changeLetter("*aCTzb1B cd/e2f");
    }

    public static void changeLetter(String str) {
        int index = 0;
        char[] array = str.toCharArray();
        for (int i = 0; i < array.length; i++)
        {
            if (array[i] >= ‘A‘ && array[i] <= ‘Z‘)// 判断大写
            {
                index = i;
                break;
            }
        }
        String start = str.substring(0, index + 1).toLowerCase();
        String end = str.substring(index + 1).toUpperCase();
        str = start + end;
        System.out.println(str);
    }
}

技术分享图片

 

JAVA-String类的应用

原文:https://www.cnblogs.com/BruceKing/p/13323763.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!