java String(Buffer&Builder)类的常用构造器和方法

String常用构造器:

        //创建空字符串
        String a1="";
        String a2=new String();

        String a3="abcd";

        char[] c={'a','b','c','d'};
        String a4=new String(c);

        byte[] b={97,98,99,100};
        String a5=new String(b);

        String a6=new String("abcd");

        System.out.println(a1);
        System.out.println(a2);
        System.out.println(a3);
        System.out.println(a4);
        System.out.println(a5);
        System.out.println(a6);

String类的常用方法:

        String a7="abcd1234567skrtnszxuifghfjh";

        //获取字符串长度
        System.out.println(a7.length());

        //根据下标找字符
        System.out.println(a7.charAt(6));

        //找第一次出现指定字符的下标
        System.out.println(a7.indexOf("z"));

        //最后一次出现指定字符的下标
        System.out.println(a7.lastIndexOf("6"));

        //判断是否为空字符串
        System.out.println(a7.isEmpty());
        System.out.println(a1.isEmpty());

        //判断字符串的开头
        System.out.println(a7.startsWith("abc"));
        System.out.println(a7.startsWith("aaa"));

        //判断字符串的结尾
        System.out.println(a7.endsWith("fjh"));
        System.out.println(a7.endsWith("fff"));

        //转为大写
        System.out.println(a7.toUpperCase());

        //转为小写
        System.out.println(a7.toLowerCase());

        //拼接 和+一样
        System.out.println(a7.concat("AASSDD"));

        //以指定字符分割字符串 分割为字符串数组
        String[] s=a7.split("n");

        //字符串截取 下标小于3的都不要
        System.out.println(a7.substring(3));

        //只要下标3到7
        System.out.println(a7.substring(3,8));

        //字符替换
        System.out.println(a7.replace("a","BB"));

        //取消前后的空格
        String a8="   1234  dfgh TYUJ   ";
        System.out.println(a8.trim());

        String a9="cdef";
        //转化为char数组
        char[] chars=a9.toCharArray();
        for (char i:chars){
            System.out.println(i);
        }

        //转化为字节数组
        byte[] bytes=a9.getBytes();
        for (byte i:bytes){
            System.out.println(i);
        }

        //比较 结果为0相等; 结果为整数 当前的大; 结果为负 被计较的大
        String a10="abce1234567";
        String a11="abcd1234567";
        System.out.println(a10.compareTo(a11));

        //重写toString()方法  返回的是内容
        String a12="123asd";
        String a13="123asd";
        System.out.println(a12.toString());

        //重写hashCode() 返回的是内容的地址
        System.out.println(a12.hashCode());
        System.out.println(a13.hashCode());

        //重写equals()方法 比较的是内容的地址 不再是对象的地址(比较内容)
        System.out.println(a12.equals(a13));

Stringbuffer&StringBuilder常用方法:

        StringBuffer s=new StringBuffer("456aaasassddff");
        String s1="456aaasassddff";

        //每次操作完后直接覆盖原本的值
        //拼接
        System.out.println(s.append("999"));
        System.out.println(s);

        //替换 将连续的一部分替换为指定字符串
        System.out.println(s.replace(3,7,"dashibi"));

        //反转
        System.out.println(s.reverse());

        //删除 删除指定部分字符串
        System.out.println(s.delete(2,5));

        //插入 在指定位置前插入字符串
        System.out.println(s.insert(14,"++"));

        //toString重写 返回此字符串
        System.out.println(s.toString());
        System.out.println(s1.toString());

StringBuffer和StringBuilder的构造器和方法一模一样

二者的区别为:StringBuffer是线程安全的,执行效率低,StringBuilder是线程不安全,执行效率高。

二者与String的区别为: String字符串为常量,不能改,而StringBuffer和StringBuilder不是常量,通过方法修改后直接覆盖原来的字符串。

String用compareTo方法比较时 返回值不只是-1、0、1, 和时间比较不同!!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值