String类概述

java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实 例。 类 String 中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻 译为大写或小写的所有字符的字符串的副本。

字符串特点:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
  • 字符串不变:字符串的值在创建后不能被更改。
  • 因为String对象是不可变的,所以它们可以被共享。
  • 字符串"abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。底层是byte[] 字节数组

使用步骤

查看类

  • java.lang.String :此类不需要导入。

查看构造方法

  • public String() :初始化新创建的 String对象,以使其表示空字符序列。
  • public String(char[] value) :通过当前参数中的字符数组来构造新的String。
  • public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的 String

注意:

  • 创建对象还一种方法就是直接创建;例如:String  s = "asd";

创建字符串对象的常见方式:

package demo01;

public class Demo01String {
    public static void main(String[] args) {
        // 方式一:使用空参构造,创建对象
        String s1 = new String();
        System.out.println(s1);
        //方式二: 根据字符数组创建字符串对象
        char[] c = {'j', 'a', 'v', 'a'};
        String s2 = new String(c);
        System.out.println(s2);
        // 方式三:根据字节数组创建字符串
        byte[] b = {97, 98, 97, 99};
        String s3 = new String(b);
        System.out.println(s3);
        //方式4:直接写字符串对象
        String s4 = "java";
        System.out.println(s4);
    }

}

与字符串比较相关的方法

  • public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
  • public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小 写。

案例演示

package Demo2;

public class Demo01StringEquals {
    public static void main(String[] args) {
        String s1 = "abcd";
        String s2 = "Abcd";
        String s3 = "abcd";
        char[] c = {'a', 'b', 'c', 'd'};
        String s4 = new String(c);
        // 比较字符串的内容是否相等
        System.out.println(s1.equals(s2));//false
        //区分大小写
        System.out.println(s2.equals(s1));//false
        System.out.println(s1.equals(s3));//true
        System.out.println(s1.equals(s4));//true
        //忽视大小写
        System.out.println(s1.equalsIgnoreCase(s2));//true
        System.out.println(s2.equalsIgnoreCase(s4));//true

    }
}

与获取相关的常用方法 

  • public int length () :返回此字符串的长度。
  • public String concat (String str) :将指定的字符串连接到该字符串的末尾。
  • public char charAt (int index) :返回指定索引处的 char值。
  • public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。如果找不到就返回-1

案例演示

package Demo2;

public class Demo02StringGet {
    public static void main(String[] args) {
        String s = "asdaedadaeqwaedaq12325";
        //获取字符串的长度,也就是字符的个数
        int length = s.length();
        System.out.println(length);// 22
        // 拼接新的字符串
        String s1 = s.concat("java");
        System.out.println(s1);//asdaedadaeqwaedaq12325java
        // 获取指定位置(index)的单个字符
        char c = s.charAt(5);
        System.out.println(c);//d
        // 查找子字符串第一次出现的位置,没有就返回-1
        int i = s.indexOf("ed");
        System.out.println(i);//4
        // 找不到返回-1
        System.out.println(s.indexOf(1));//-1

    }
}

与字符串截取相关的方法

  • public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。
  • public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex

案例演示

package Demo2;

public class Demo03Substring {
    public static void main(String[] args) {
        //截取字符串
        String s1 = "abcdefg123";
        String s2 = s1.substring(3);
        String s3 = s1.substring(3, 6);
        System.out.println(s1);//abcdefg123
        System.out.println(s2);//defg123
        System.out.println(s3);//def
    }
}

与字符串转换相关的方法

  • public char[] toCharArray () :将此字符串转换为新的字符数组。
  • public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
  • public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。
package Demo2;

public class Demo04StringConvert {
    public static void main(String[] args) {
        String s1 = "abcdABCDabcd";
        // 转换为字符数组
        char[] c = s1.toCharArray();
        //遍历数组
        for (int i = 0; i < c.length; i++) {
            System.out.print(c[i] + " ");//a b c d A B C D a b c d
        }
        //转换为字节数组
        byte[] b = s1.getBytes();
        //遍历字节数组
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");//97 98 99 100 65 66 67 68 97 98 99 100
        }
        //替换指定位置的字符
        String s2 = s1.replace("a", "好");
        System.out.println(s2);//好bcdABCD好bcd
    }
}

字符串分割的方法:

  • public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组
package Demo2;

public class Demo05StringSplit {
    public static void main(String[] args) {
           //创建字符串对象
        String s = "aaxbbxcc";
        String[] strArray = s.split("x");
        for (int x = 0; x < strArray.length; x++) {
            System.out.print(strArray[x] + " "); // aa bb cc
        }
    }
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄