字符串反转

本文包含四个Java程序示例,分别涉及字符串反转、用户信息输入验证、字符串格式化输出和字符计数。第一个程序展示了如何在不使用内置函数的情况下反转字符串。第二个程序检查用户名、密码和邮箱的输入格式是否正确。第三个程序接收一个名字并按特定格式输出。第四个程序统计字符串中大小写字母和数字的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、将字符串反转

String 是一种不可变字符串,一对字符串进行改变操作,则创建一个全新的对象,然后把引用指向这个新的对象。

package enumeration;


public class HomeWork {

    public static void main(String[] args) {
        String str  = "abcdefg";
        str = Me.reverse(str,0,3);
        System.out.println(str);

    }





}
class Me {


    public static String reverse(String str, int start, int end) {
        //对输入的参数做一个验证
        //写出正确的情况,然后取反。
        if (!(str != null && start > 0 && end <= str.length() && start < end)){
            System.out.println("输入有误");
            throw new RuntimeException("参数不正确");

        }
        char[] chars = str.toCharArray();
        //改变成数组
        char temp  = ' ';
        for (int i = start ,j = end; i < j; i++, j--) {
            temp = chars[i];
            chars[i] = chars[j ];
            chars[j] = temp;

        }
        //以上是对字符串数组进行修改。
        String s = new String(chars);
        //String 是一种不可变字符串,一对字符串进行改变操作,则创建一个全新的对象,然后把引用指向这个新的对象。
        //将chars字符串数组转换成String
//        return new String(chars);
        return s;
    }
}

2、输入信息 

package enumeration;

import java.util.Scanner;

public class HomeWork01 {
    public static void main(String[] args) {
//
        String name = "jack";
        String password = "123456";
        String mail = "1152@qq.com";
        try {
            apply(name,password,mail);
            System.out.println("注册成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void apply(String name ,String password ,String mail){



        if (!(name.length()==2 || name.length() == 3 || name.length() ==4) ){
            throw new RuntimeException("用户名的长度为2或3或4");
        }


        if (!(password.length() == 6 && isDegital(password) )){
            throw new RuntimeException("输入格式有误");
        }

        int i = mail.indexOf(".");
        int j = mail.indexOf("@");
        if (!(i > 0 && j < i)){
            throw new RuntimeException("输入格式有误");
        }

        System.out.println("信息输入正确,如下:");
        System.out.println("name = " +name + 
                "password = " + password +
                "mail = " + mail);


    }
    public static boolean isDegital(String password){
        //将password转换成字符串数组
        char[] chars = password.toCharArray();
        for (int i = 0; i < chars.length ; i++) {
            //用来判断是否是一个数字,解释:看当前的数组的ASCII是否小于0和9的ascii码,要是
            //超出范围表示不是一个数字。
            if (chars[i] < '0' && chars[i] > '9' ){
                return false;
            }
        }

        return true;
    }
}

3、分割字符串,改变输出的格式

package enumeration;

public class M {
    public static void main(String[] args) {
        String name = "wang xiao sai";
        printName(name);
    }

    public static void printName(String str){
        if (str == null){
            System.out.println("str 不能为null");
            return;
        }
        String[] names = str.split(" ");//进行分割,分割之后保存为字符数组。
        if(names.length != 3){
            System.out.println("结束啦");
            return;
        }
        String format = String.format("%s %s .%c",names[2],names[0],names[1].toUpperCase().charAt(0));//最后一句话进行解读:将最后的名字先转成大写字母,然后取第一个字符。
        System.out.println(format);
    }
}

4、查找多少个大小写字符数字

package enumeration;

public class G {
    public static void main(String[] args) {
        String str = "uucahsuilcha123";
        find(str);
    }
    public static void find(String str){
        int smallCount = 0;
        int maxCount = 0;
        int numCount = 0;
        if (str == null){
            throw new RuntimeException("字符串不能为null");
        }
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] > '0' && chars[i] < '9'){
                numCount++;
            }
            if (chars[i] > 'a' && chars[i] < 'z'){
                smallCount++;
            }
            if (chars[i] > 'A' && chars[i] < 'Z'){
                maxCount++;
            }
        }
        System.out.println("numCount" + numCount + "\tsmallCount" + smallCount + "\tmaxCount" + maxCount);
    }
}

FTTFFT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值