String类

String类


  • 概述

特点


  1. 字符串不变:字符串的值在创建后不能被更改。[[值传递和引用传递#String类型|String类引用传递]]
package com.zhou.day0822;  
  
//String类  
public class Demo01 {  
    public static void main(String[] args) {  
  
        //只要是引用类型的数据    new出来的都在堆内存中  
        int a = 10;  
  
        //String类型可new可不new  分情况分析  
        //String s = new String();  
        //String s = "aa";//  
        //1.字符串不变:字符串的值在创建后不能被更改。  
        String s = "狗子";  
        //一个引用类型变量默认情况下打印的都是内存地址  
        System.out.println(s);  
        s = s + "bb";  
        System.out.println(s);  
    }  
}

  • 上图一个创建了三个对象,但是s的地址从始至终都没有变。此即String类的值不可变性!
  1. 因为String对象是不可变的,所以它们可以被共享。
//1.字符串不变:字符串的值在创建后不能被更改。  
String s = "狗子";  
//一个引用类型变量默认情况下打印的都是内存地址  
System.out.println(s);  
//s = s + "bb";  
System.out.println(s);  
  
//2.因为String对象是不可变的,所以它们可以被共享  
String s1 = "狗子";  
System.out.println(s == s1);//true

3. "abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。

//3. "abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。  
char[] c = {'a','b','c'};  
String str = new String(c);  
System.out.println(str);

使用步骤


package com.zhou.day0822;  
  
public class Demo03 {  
    public static void main(String[] args) {  
        //无参构造  
        String str = new String();  
  
        //通过字符数组构造  
        char[] c = {'a','b','c'};  
        String str2 = new String(c);  
  
        //通过字节数组构造  
        byte[] bytes = {97,98,99};  
        String str3 = new String(bytes);  
    }  
}

常用方法


  1. 判断功能的方法:
    1. public boolean equals(Object anObject):将此字符串与指定对象进行比较。
public class Demo04 {  
    public static void main(String[] args) {  
        String s1 = "Hello";  
        String s2 = "Hello";  
        String s3 = "HEllo";  
        String s4 = new String("Hello");  
        System.out.println(s1 == s2);  
        //只要内存地址一样   两个变量内容肯定一样   反过来不成立  
        System.out.println(s1.equals(s2));  
        String s5 = "He";  
        s5 = s5+"llo";  
        System.out.println(s5.equals(s1));//true  
        System.out.println(s5 == s1);//false  
    }  
}
  1. public boolean equalsIgnoreCase(String anotherString):将此字符串与指定对象进行比较,忽略大小写。
public class Demo04 {  
    public static void main(String[] args) {  
        String s1 = "Hello";  
        String s2 = "Hello";  
        String s3 = "HEllo";  
        System.out.println(s2.equalsIgnoreCase(s3));//true
  1. 获取功能的方法
    1. public int length();:返回此字符串的长度。
    2. public String concat(String str);:将指定的字符串连接到该字符串的末尾。
    3. public char charAt(int index);:返回指定索引处的char值。
    4. public int indexOf(String str);:返回指定子字符串第一次出现在该字符串内的索引。
    5. public String substring(int beginIndex);:返回一个子字符串,从beginindex开始截取字符串到字符串结尾。
    6. public String substring(int brginIndex,int endIndex);:返回一个子字符串,从beginindex到endindex截取字符串。含beginindex,不含endindex。(左闭右开)
    • 方法演示,代码如下:
package com.zhou.day0822;  
  
public class Demo05 {  
    public static void main(String[] args) {  
        //1.获取字符串的字符个数  
        String s = "helloworldhello";  
        System.out.println(s.length());  
  
        //2.将指定的字符串连接到该字符串的末尾。  
        s = s.concat("666");  
        System.out.println(s);//helloworldhello666  
  
        //3.返回指定索引处的char值  
        char c = s.charAt(5);  
        System.out.println(c);  
  
        //4.返回指定子字符串第一次出现在该字符串内的索引。  
        int i = s.indexOf("hello");  
        System.out.println(i);  
  
        //5.返回一个子字符串,从beginindex开始截取字符串到字符串结尾。  
        String str = "yedemingmingshu";  
        System.out.println(str.substring(4));  
  
        //6.返回一个子字符串,从beginindex到endindex截取字符串。含beginindex,不含endindex。(左闭右开)  
        String str1 = "jishizaixiaodefanyenengyuanhang";  
        System.out.println(str1.substring(2,7));  
    }  
}
  1. 转换功能的方法
    1. public char[] toCharArray();:将此字符串转换为新的字符数组。
    2. public byte[] getBytes();:适用平台默认字符集将该String编码转换为新的字节数组。
    3. public String replace(CharSequence target,CharSequence replacement);:将于target匹配的字符串使用replacement字符串替换。
    • 方法演示,代码如下:
public class Demo06 {  
    public static void main(String[] args) {  
        //1.将此字符串转换为新的字符数组。  
        String str = "abcabc";  
        char[] c = str.toCharArray();  
        for (int i = 0;i<c.length;i++) {  
            System.out.println(c[i]);  
        }  
  
        //2.适用平台默认字符集将该String编码转换为新的字节数组。  
        byte[] b = str.getBytes();  
        for (int i = 0;i<b.length;i++) {  
            System.out.println(b[i]);  
        }  
  
        //3.将于target匹配的字符串使用replacement字符串替换。  
        String s = str.replace("bc","aa");  
        System.out.println(s);  
    }  
}
  1. 分割功能的方法
    1. public String[] split(String regex);:将此字符串按照给定的regex(规则)拆分为字符串数组。
    • 方法演示,代码如下:
package com.zhou.day0822;  
  
public class Demo07 {  
    //1.分割功能的方法  
    public static void main(String[] args) {  
        String s = "aa-bb-cc-dd";  
        String[] str = s.split("-");  
        for (int i = 0;i<str.length;i++) {  
            System.out.println(str[i]);  
        }  
    }  
}
posted @ 2022-08-22 11:46  小周和java  阅读(11)  评论(0编辑  收藏  举报