String常用方法

public static void main(String[] args) {
        String a="defrag";
        String b="defrag";
        String c="Defrag";
        String d=" D efra ge ";
        //获取字符串的长度
        System.out.println("1."+a.length());
        //获取指定字符在对象中出现的第一次索引,如果没有返回-1
        System.out.println("2."+a.indexOf("d"));
        //获取指定索引处的字符
        System.out.println("3."+a.charAt(1));
        //从输入的索引位置开始截取字符串
        System.out.println("4."+a.substring(4));
        //从1号索引位置到4号索引位置,包括1号索引但不包括4号索引
        System.out.println("5."+a.substring(1,4));
        //比较字符串的内容是否相同
        System.out.println("6."+a.equals(c));
        //比较字符串的内容是否相同忽略大小写
        System.out.println("7."+a.equalsIgnoreCase(c));
        //判断字符串对象是否以指定的字符开头(区分大小写)
        System.out.println("8."+a.startsWith("d"));
        //判断字符对象是否以指定的字符开头(区分大小写)
        System.out.println("9."+a.startsWith("f", 2));
        //判断字符串对象是否以指定的字符结尾
        System.out.println("10."+a.endsWith("f"));
        //判断指定字符串是否为空
        System.out.println("11."+a.isEmpty());
        //比较字符串的大小,前者大返回整数,后者大返回负数,相等返回0.
        System.out.println("12."+a.compareTo(c));
        //把字符串转换为字符数组
        System.out.println("13."+a.toCharArray());
        //把字符串转换为小写字符串
        System.out.println("14."+c.toLowerCase());
        //把字符串转换为大写字符串
        System.out.println("15."+a.toUpperCase());
        //去除字符串两端的空格
        System.out.println("16."+d.trim());
        //去除字符串种指定的字符,然后返回结果是个数组,需要遍历这个数组才能打印出来
        for (String s : b.split("f|a")) {
            System.out.print(s);
        }
        //截取字符串中指定位置的字符组成一个新的字符串,包括头部不包括尾部
        System.out.println();
        System.out.println("18."+a.subSequence(2,4));
        //将指定字符替换成另一个指定的字符
        System.out.println("19."+a.replace("e", "b"));
        //用新的内容替换全部旧内容
        System.out.println("20."+a.replaceAll("efr","bbb"));
        //6)replaceFirst(String regex,String replacement):替换首个满足条件的内容
        System.out.println("21."+d.replaceFirst("e","aa"));
        //7)lastIndexOf(String str):返回指定字符出现的最后一次的下标
        System.out.println("22."+d.lastIndexOf("e"));
        //8)contains(CharSequence s):查看字符串中是都含有指定字符
        System.out.println("23."+a.contains("e"));
        //9)concat(String str):在原有的字符串的基础上加上指定字符串
        System.out.println("24."+a.concat("aaa"));
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨流年666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值