JavaSE——String类练习

目录

1.字符串反转 

2.字符串判断

3.字符串统计

4.判断题


1.字符串反转 

将字符串中指定部分进行反转。

public static void main(String[] args) {
        // 字符串反转
        String str = "abcdef";
        System.out.println("交换前:" + str);
        try {
            str = reverse(str, 4, 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }
        System.out.println("交换后:" + str);
    }

    /**
     * (1) 将字符串中指定部分进行反转。比如将"abcdef"反转为"aedcbf"
     * (2) 编写方法 public static String reverse(String  str, int start , int end) 搞定
     * 思路分析:
     * (1) 先把方法定义确定
     * (2) 把 String 转成 char[] ,因为char[] 的元素是可以交换的
     * (3) 画出分析示意图
     * (4) 代码实现
     */
    public static String reverse(String str, int start, int end) {
        // 对输入的参数做一个验证:
        // (1) 写出正确的情况
        // (2) 然后取反即可
        // (3) 这样写,思路就不乱
        if (!(str != null && start >= 0 && end > start && end < str.length())) {
            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;
        }

        // 使用chars重新构建字符串
        return new String(chars);
//        return chars.toString();
//        char[](字符数组)类本身并不重写 Object 类的 toString() 方法,
//        因此调用 chars.toString() 时返回的是数组对象的默认字符串
    }

2.字符串判断

        输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象。
要求:
1)用户名长度为2或3或4
2)密码长度为6,要求全是数字 isDigital
3)邮箱中包含@和.并且@在.前面

public static void main(String[] args) {
    String username = "zhan";
    String password = "123456";
    String email = "123456@qq.com";
    try {
        UserRegister( username,  password,  email);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

public static void UserRegister(String username, String password, String email) {
    if (username == null || password == null || email == null) {
        throw new RuntimeException("参数不能为null");
    }

    // 判断username
    int length = username.length();
    if (!(length >= 2 && length <= 4)) {
        throw new RuntimeException("用户名长度错误!");
    }

    // 判断password
    boolean digital = isDigital(password);
    if (!digital){
        throw new RuntimeException("密码长度为6,要求全是数字");
    }

    // 判断email
    int i = email.indexOf(".");
    int j = email.indexOf("@");
    if(!(i > j && i > 0)){
        throw new RuntimeException("邮箱中包含@和.并且@在.前面");
    }
}

public static boolean isDigital(String password) {
    char[] chars = password.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] < '0' && chars[i] > '9') {
            return false;
        }
    }
    return true;
}

3.字符串统计

        输入David joseph Beckham的人名,以Beckham,David .J的形式打印出来,其中.J是中间单词的首字母。 

public static void main(String[] args) {
    String name = "David joseph Beckham";
    printName(name); // Beckham,David .J
}

private static void printName(String name) {
    if (name == null) {
        System.out.println("name 不能为空");
        return;
    }

    String[] split = name.split(" ");
    if (split.length != 3) {
        System.out.println("输入的字符串格式不对!");
        return;
    }
    String format = String.format("%s,%s .%c", split[2], split[0], split[1].toUpperCase().charAt(0));
    System.out.println(format);
}

4.判断题

public static void main(String[] args) {
    String s1 = "zhangsan";
    Animal a = new Animal(s1);
    Animal b = new Animal(s1);
    System.out.println(a == b); // false
    System.out.println(a.equals(b)); // false  Animal没有重写equals方法,所以比较的是地址
    System.out.println(a.name == b.name); // true  指向常量池同一个地址

    String s4 = new String("zhangsan");
    String s5 = "zhangsan";

    System.out.println(s1 == s4); // false 先在堆中创建value,再指向字符串常量池
    System.out.println(s4 == s5); // false

    String t1 = "hello" + s1;
    String t2 = "hellozhangsan";
    System.out.println(t1.intern() == t2); // true t1.intern()返回指向常量池的结果
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值