java-String类、StringBuffer类与StringBuilder类

String类

String类实例化

  • 字面量方式定义
        String s1="hello";
  • 通过new+构造器的方式
        String s2=new String("world");
        char[] chars={'r','e','f','i','n','e'};
        String s4=new String(chars);

String不同拼接操作

  • 常量与常量的拼接结果在常量池;常量池不会存在相同内容;
  • 只有有一个结果是变量,结果就在堆中;
        String s="HelloWorld";
        final String s1="Hello";
        String s2="Hello";
        String s3="World";
        String s4 = s1 + "World";
        String s5="Hello"+"World";
        String s6=s2+"World";
        String s7=s2+s3;
        System.out.println(s==s4);//true 比较地址值
        System.out.println(s4==s5);//true
        System.out.println(s==s5);//true
        System.out.println(s==s6);//false
        System.out.println(s==s7);//false
        System.out.println(s6==s7);//false

String类常用的方法

  • length():返回字符串长度
        String s="  Refine   ";
        String s1="refine";
        String s2=new String("refine");
        System.out.println(s.length());
  • charAt(int n):返回指定索引所在的字符
        System.out.println(s.charAt(2));
  • isEmpty():判断字符串是否为空
        System.out.println(s.isEmpty());
  • toLowerCase():字符串转为小写
        System.out.println(s.toLowerCase());
  • toUpperCase():字符串转为大写
        System.out.println(s.toUpperCase());
  • trim():去除字符串前后空格,返回一个新的字符串
        String s3=s.trim();
        System.out.println(s.trim());
  • equals():判断两个字符串是否相等
        System.out.println(s1.equals(s2));
  • equalsIgnoreCase():忽略大小写判断两个字符串是否相等
        System.out.println(s.trim().equalsIgnoreCase(s1));
  • concat():拼接字符串
        System.out.println(s.concat("sophia"));
  • compareTo():比较两个字符串的大小
        System.out.println(s.compareTo(s1));
  • substring():截取字符串,返回一个子字符串
        System.out.println(s1.substring(2));
  • endsWith():判断字符串是否以某个字符结尾
        String s="Hello World!";
        System.out.println(s.endsWith("!"));
  • startsWith():判断字符串是否以某个字符开头
        System.out.println(s.startsWith("e"));
        //从第n个索引开始计算,字符串是否以某个字符开头
        System.out.println(s.startsWith("e", 1));
  • contains():判断字符串是否包含指定字符、字符串
        System.out.println(s.contains("el"));
  • indexOf():返回指定字符所在的索引位置
        System.out.println(s.indexOf("l"));
  • lastIndexOf():返回指定字符最后一次出现的索引位置
        System.out.println(s.lastIndexOf("e", 4));
  • replace():使用指定字符串C替换字符串A中原有字符串B
        String s=new String("Hello world!上海");
        System.out.println(s.replace("l","L"));
        System.out.println(s.replace("上海", "安徽"));
  • replaceAll():正则表达式的方式替换原有字符串
        String s1="3433mysql123linux343java234python4556jmeter458";
        String s2 = s1.replaceAll("\\d+", ",");
        String s3 = s2.replaceAll("^,|$,", "");
        System.out.println(s3);

String类与char[]相互转换

String类型转换为char[]
        //String类型转换为char[]
        String s=new String("Hello world!上海");
        char[] chars=s.toCharArray();
        for (char ch:chars) {
            System.out.println(ch);
        }
char[]转为String
        //char[]转为String
        char[] chars1=new char[]{'a','b','c'};
        String s1=new String(chars1);
        System.out.println(s1);

### String类与bytes[]相互转换

String类型转换为bytes[]
        //String类型转换为bytes[]
        String s=new String("Hello world!得到");
        byte[] bytes = s.getBytes();//使用默认字符集编码
        System.out.println(Arrays.toString(bytes));
        
        byte[] bytes1=null;
        try {
            bytes1 = s.getBytes("gbk");//使用gbk字符集进行编码
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Arrays.toString(bytes1));
bytes[]类型转换为String类型
        //bytes[]类型转换为String类型
        String s1 = new String(bytes);//使用默认字符集,进行解码
        System.out.println(s1);

        String s2 = null;
        try {
            s2 = new String(bytes1,"gbk");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        System.out.println(s2);

String类、StringBuffer类与StringBuilder类的比较

  • String:是不可变的字符序列;底层使用char[]存储
  • StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[]存储
  • StringBuilder:可变的字符序列;线程不安全的,效率高;底层使用char[]存储

StringBuffer常用的方法

  • append():添加字符、字符串
        StringBuffer stringBuffer=new StringBuffer("Refine");
        stringBuffer.append('D');
        stringBuffer.append("David");
  • delete():删除指定开始索引、结束索引位置的字符串;
        stringBuffer.delete(7,8);
  • replace():替换指定索引范围内的字符串
        System.out.println(stringBuffer.replace(8,9,"LL"));
  • insert():在指定索引位置插入字符串
        stringBuffer.insert(0,"HELLO");
  • reverse():反转
        System.out.println(stringBuffer.reverse());
  • indexOf():返回指定字符的索引
        System.out.println(stringBuffer.indexOf("H"));
  • substring():截取指定索引位置的字符串,返回为一个新的字符串
        System.out.println(stringBuffer.substring(2,5));
  • length():返回字符串长度
        System.out.println(stringBuffer.length());
  • charAt():返回指定索引位置的字符
        System.out.println(stringBuffer.charAt(1));
  • setCharAt():在指定索引位置设置字符
        stringBuffer.setCharAt(1,'U');
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梅尝酥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值