关于String的一些常用方法的编程练习题

在这里插入图片描述

    public static void main(String[] args) {
        /**
         * 思路分析
         * 1.编写方法public static String reverse(String str, int start, int end) {
         * 2.把String 转成 char[],因为char[] 的元素是可以交换的
         * 3.代码实现
         */
        String s = "abcdef";
        System.out.println("交换前================");
        System.out.println(s);
        System.out.println("交换后=================");

        try {
             s = reverse(s, 1, 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }
        System.out.println(s);

    }

    public static String reverse(String str, int start, int end) {
        //对输入的参数做一个验证
        //这里分享一个编程技巧
        //1.凡是涉及到错误校验情况的,可以先写出正确的情况(因为容易想到)
        //2.然后整体再取反,即可完成错误校验的代码部分
        if(!(str!=null && start<end && start >= 0 && end <str.length())){
            throw new RuntimeException("参数不正确");
        }
        char[] sArr = str.toCharArray();
        char temp = ' ';
        for (int i = start,j= end; i < j; i++,j--) {//注意边界条件的设置
            temp = sArr[i];//辅助变量
            sArr[i] = sArr[j];
            sArr[j] = temp;
        }
        //注意不能返回str,因为String是被final修饰的,所以要重新构建
//        String res = sArr.toString();这样是不行的哈
        return new String(sArr);

    }



//输出结果如下,如果输入的参数正确的话
交换前================
abcdef
交换后=================
aedcbf
//如果输入的参数有误,则会输出
交换前================
abcdef
交换后=================
参数不正确

在这里插入图片描述

public class Journey {
    public static void main(String[] args) {
        String username = "kere";
        String passward = "123456";
        String email = "123@qq.com";

        try {
            userRegister(username,passward,email);
            //如果没有抛出异常,说明注册成功了
            System.out.println("恭喜你 注册成功!!!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


    }
    public static void userRegister(String name,String pwd,String email){
        //再升级下,多加些校验机制
        if(!(name!=null&&pwd!=null&&email!=null)){
            throw new RuntimeException("参数不能为空哟!!!");
        }
        //过关
        //用户名长度为2、3、4
        int nameLength = name.length();
        if(!(nameLength>=2&&nameLength<5)){
            throw new RuntimeException("用户名长度不正确!!!");
        }

        //第二关,密码长度为6,要求全是数字
        if(!(pwd.length()==6&&digital(pwd))){
            throw new RuntimeException("密码长度不正确或密码中包含有非法字符");
        }

        //第三关,@在.之前
        int index1 = email.indexOf("@");
        int index2 = email.indexOf(".");
        if(!(index1>-1 && index1<index2)){
            throw new RuntimeException("邮箱格式错误");
        }
    }
    public static boolean digital(String pwd){
        char[] pwdArr = pwd.toCharArray();
        for (int i = 0; i < pwdArr.length; i++) {
            if(!(pwdArr[i]<'9'&&pwdArr[i]>'0')){
                return false;
            }
        }
        return true;
    }

}

在这里插入图片描述

public class Journey {
    public static void main(String[] args) {
        //以Joe, Dss .K的形式打印出来  其中.K 是中间单词的首字母大写
        String name = "Dss kerwin Joe";
        printName(name);


    }
    //编写方法,完成输出格式要求的字符串

    /**
     * 思路分析:
     * 1.对输入的字符串进行分割split("")
     * 2.对得到的String[] 进行格式化String.format()
     * 3.对输入的字符串进行校验即可
     */
    public static void printName(String str) {
        if (str == null) {
            System.out.println("str 不能为空哟");
            return;
        }
        String[] s = str.split(" ");
        if (s.length != 3) {//这种容易想到的,就不要刻意去照搬之前的校验技巧了,之前的校验技巧是情况复杂时,先写出正确的,最后整体取反
            System.out.println("输入的名字格式不正确!!!");
            return;
        }
        //格式化,使之按照Joe, Dss .K 输出
        String formatRes = String.format("%s, %s .%c", s[2], s[0], s[1].toUpperCase().charAt(0));
        System.out.println(formatRes);
    }
}

在这里插入图片描述

public class Journey {
    public static void main(String[] args) {
        String youth = "ABjso340xW";
        countStr(youth);
        System.out.println("=============");
        String ess = "csijdcbsljdbv23bux899AAA";
        countStr(ess);
        System.out.println("=======================");
        String ker = null;
        try {
            countStr(ker);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
    public static void countStr(String str){
        if(str==null){
            throw new RuntimeException("给定的参数不能为空哟!!!");
        }
        char[] youthAAA = str.toCharArray();
        int NumCount = 0;
        int UpperCount = 0;
        int LowerCount = 0;
        for(char i : youthAAA){
            if(i<='9'&&i>='0'){
                NumCount++;
            }else if(i<='z'&&i>='a'){
                LowerCount++;
            }else if(i<='Z'&&i>='A'){
                UpperCount++;
            }
        }
        System.out.println("字符串中有"+NumCount+"个数字");
        System.out.println("字符串中有"+UpperCount+"个大写字母");
        System.out.println("字符串中有"+LowerCount+"个小写字母");
    }
}

//输出结果
字符串中有3个数字
字符串中有3个大写字母
字符串中有4个小写字母
=============
字符串中有5个数字
字符串中有3个大写字母
字符串中有16个小写字母
=======================
给定的参数不能为空哟!!!
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值