字符串替换、反转、查找、转大写、删除其中一个

String str = "finished with exit finished";
 //replace
        System.out.println(str.replace('i', 'a'));
        System.out.println(str.replaceFirst("fi", "ti"));
        System.out.println(str.replaceAll("i", "a"));

        //fanashed wath exat fanashed
        //tinished with exit finished
        //fanashed wath exat fanashed

        //截取字符串
        System.out.println(str.substring(5));
        System.out.println(str.substring(2, 5));

        // result: hed with exit finished
        //result: nis


        //字符串反转
        String reverse = new StringBuffer(str).reverse().toString();
        System.out.println("字符串反转后:" + reverse);
        //result: 字符串反转后:dehsinif tixe htiw dehsinif


        //字符串查找
        int intIndex = str.lastIndexOf("exit");
        if (intIndex == -1) {
            System.out.println("没有找到字符串: exit");
        } else {
            System.out.println("exit字符串位置为:" + intIndex);
        }
        //result: exit字符串位置为:14


        //查找字符串最后一次出现的位置
        int lastIndex = str.lastIndexOf("exit");
        if (lastIndex == -1) {
            System.out.println("没有找到字符串: exit");
        } else {
            System.out.println("exit字符串最后出现的位置为:" + lastIndex);
        }
        //result:14


        //字符串小写转大写
        String capitalLetters = str.toUpperCase();
        System.out.println(capitalLetters);
        //result: FINISHED WITH EXIT FINISHED


        // 删除字符串中的一个字符
        System.out.println(removeCharAt(str, 7));

    }

    private String removeCharAt(String str, int i) {
        return str.substring(0, 7) + str.substring(i + 1);
        //result: finishe with exit finished

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值