常用包装类之String类以及String类的常用方法

String

  • 内存中常量,值一旦在内存中创建,不可更改

package com.bz.test;
​
public class Test6 {
    public static void main(String[] args) {
        String a = "abc";
        String b = a;
​
        System.out.println("a:"+a);//abc
        System.out.println("b:"+b);//abc
        System.out.println(a == b);//t
​
        b = "edf";
        System.out.println("a:"+a);//abc
        System.out.println("b:"+b);//edf
        System.out.println(a == b);//f
​
    }
}
​

什么是串池?

全称字符串常量池。String是实际开发中使用频率最高的数据类型,其中对字符串内容的复用率也很高,为了降低复用字符串带来的内存压力,所以在方法区中设立了串池。

创建

  1. 直接双引号赋值

    String 引用名="值";
  2. 通过构造赋值

    String 引用名=new String("值");

区别

  1. 第一种创建方式会优先使用串池,赋值时先去串池中查找是否存在该字符串,如果存在,则直接引用,如果不存在,则在串池中创建该字符串然后引用,更加节省内存空间

  2. 第二种创建方式不会优先使用串池,必定开辟对象空间。如果字符串在串池中存在,则直接让对象空间引用该串池地址,如果字符串在串池中不存在,则在串池创建该内容,其后使对象空间引用该地址。

package com.bz.test;
​
public class Test7 {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1 == s2);//t  优先使用串池,串池地址相同
​
        String str1 = new String("edf");
        String str2 = new String("edf");
​
        System.out.println(str1 == str2);//f   一定拥有堆地址,堆地址不同
​
        String str = "edf";
        System.out.println(str1 == str);//f  str1优先使用堆地址,str使用串池地址,一定不同
​
    }
}
​

可变长字符串

  1. StringBuffer:JDK1.0 线程安全,效率低

  2. StringBuilder:JDK5.0 线程不安全,效率高

使用

  1. 只能通过构造创建

  2. 内容改变只能通过调用方法完成

        //创建一个可变长字符串
        StringBuilder sb=new StringBuilder();
        //循环拼接A-Z
        for (char i = 65; i <=90 ; i++) {
            //拼接内容至字符串末尾
            sb.append(i);
        }
        System.out.println(sb);
​
//----------------------------------------------------
        StringBuilder sb1 = new StringBuilder("abc");
        StringBuilder sb2 = sb1;
​
        sb2.append("edf");//追加edf
​
        System.out.println(sb1);//abcedf
        System.out.println(sb2);//abcedf
        System.out.println(sb1 == sb2);//t

String的常用方法

  • 引用名.方法名()

  1. char charAt(下标):获取字符串指定下标位置的元素

  2. boolean contains(字符串):判断参数内容在字符串中是否存在

  3. boolean endsWith(字符串):判断字符串是否以参数内容结尾

  4. boolean startsWith(字符串):判断字符串是否以参数内容开头

  5. boolean equals(字符串):判断字符串内容与参数内容是否相同,区分大小写

  6. boolean equalsIgnoreCase(字符串):判断字符串内容与参数内容是否相同,不区分大小写

  7. byte[] getBytes():以byte数组的形式返回字符串内容

  8. 下标 indexOf(字符串):获取参数内容第一次出现的下标,不存在返回-1

  9. 下标 lastIndexOf(字符串):获取参数内容最后一次出现的下标,不存在返回-1

  10. boolean isEmpty():判断字符串内容是否为空,不能判比null值

  11. int length():获取字符串长度

  12. 新字符串 replace(旧字符串,新字符串):将字符串中旧内容替换为新内容,返回替换结果

    • 会替换所有符合内容

  13. String[] split(分割字符串):根据参数内容对字符串进行分割,不会保留分割字符

    • 可以通过""省略分隔符,意为每个字符都是一个结果

  14. String subString(开始下标):对字符串从开始下标截取至末尾

  15. String subString(开始下标,结束下标):对字符串从开始下标截取至结束下标前一位

  16. char[] toCharArray():将字符串转换为char数组

  17. String toLowerCase():将字符串内容转全小写

  18. String toUpperCase():将字符串内容转全大写

  19. String trim():去除字符串前后空格

package com.bz.test;
​
public class Test9 {
    public static void main(String[] args) {
        String s = "hello";
        //char charAt(下标):获取字符串指定下标位置的元素
        System.out.println(s.charAt(0));//h
        //boolean  contains(字符串):判断参数内容在字符串中是否存在
        System.out.println(s.contains("el"));//t
        System.out.println(s.contains("eo"));//f
        //boolean  endsWith(字符串):判断字符串是否以参数内容结尾
        System.out.println(s.endsWith("llo"));//t
        System.out.println(s.endsWith("hello"));//t
        //boolean  startsWith(字符串):判断字符串是否以参数内容开头
        System.out.println(s.startsWith("h"));//t
        //boolean  equals(字符串):判断字符串内容与参数内容是否相同,区分大小写
        System.out.println(s.equals("HELLO"));//F
        //boolean  equalsIgnoreCase(字符串):判断字符串内容与参数内容是否相同,不区分大小写
        System.out.println(s.equalsIgnoreCase("HELLO"));//t
        //byte[]  getBytes():以byte数组的形式返回字符串内容
        byte[] bs = s.getBytes();
        for (int i = 0; i < bs.length; i++) {
            System.out.print(bs[i]+" "+(char)bs[i]+"  ");
        }
        System.out.println();
        //下标  indexOf(字符串):获取参数内容第一次出现的下标
        System.out.println(s.indexOf("l"));//2
        System.out.println(s.indexOf("k"));//2
        //下标  lastIndexOf(字符串):获取参数内容最后一次出现的下标
        System.out.println(s.lastIndexOf("l"));//3
        System.out.println(s.lastIndexOf("k"));//-1
        //boolean  isEmpty():判断字符串内容是否为空
        System.out.println(s.isEmpty());//f
        String s2 = " ";//内有一个空格
        System.out.println(s2.isEmpty());//f
        String s3 = null;
      //  System.out.println(s3.isEmpty()); 空指针
        //int   length():获取字符串长度
        System.out.println(s.length());//5
        //新字符串  replace(旧字符串,新字符串):将字符串中旧内容替换为新内容,返回替换结果
        System.out.println(s.replace("l","m"));//hemmo
        //String[]  split(分割字符串):根据参数内容对字符串进行分割,不会保留分割字符
        String str = "a,b,c,d";
        //对str根据,进行分割
        String[] ss = str.split(",");
        for (int i = 0; i < ss.length; i++) {
            System.out.print(ss[i]+" ");//a b c d
        }
        System.out.println();
​
        String[] ss2 = str.split("");
        for (int i = 0; i < ss.length; i++) {
            System.out.print(ss[i]+" ");//a , b , c , d
        }
        System.out.println();
        //String  subString(开始下标):对字符串从开始下标截取至末尾
        System.out.println(s.substring(1));//ello
        //String  subString(开始下标,结束下标):对字符串从开始下标截取至结束下标前一位
        System.out.println(s.substring(1,5));//ello
        //char[]  toCharArray():将字符串转换为char数组
        char[] cs = s.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            System.out.print(cs[i]+"  ");//h  e  l  l  o
        }
        System.out.println();
        //String  toLowerCase():将字符串内容转全小写
        String str3 = "ABCD";
        System.out.println(str3.toLowerCase());//abcd
        //String  toUpperCase():将字符串内容转全大写
        System.out.println(s.toUpperCase());//HELLO
        //String  trim():去除字符串前后空格
        String str4 = "  a bcd  ";
        System.out.println(str4.trim());//a bcd
​
    }
}
​
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值