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

使用步骤

  1. // 无参构建
  2. String str = new String();
  3. // 通过字符数组构造
  4. char chars[] = {'a', 'b', 'c'};
  5. String str2 = new String(chars);
  6. // 通过字节数组构造
  7. byte bytes[] = { 97, 98, 99 };
  8. String str3 = new String(bytes)

常用方法

判断功能的方法

  • public boolean equals (Object anObject) :将此字符串与指定对象进行比较
  • public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小 写。
    public class Test {
      public static void main(String[] args) {
          // 创建字符串对象
          String s1 = "hello";
          String s2 = "hello";
          String s3 = "HELLO";
          // boolean equals(Object obj):比较字符串的内容是否相同
          System.out.println(s1.equals(s2));
          System.out.println(s1.equals(s3));
          // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
          System.out.println(s1.equalsIgnoreCase(s2));
          System.out.println(s1.equalsIgnoreCase(s3));
      }
    }
    

获取功能的方法

  • public int length () :返回此字符串的长度。
  • public String concat (String str) :将指定的字符串连接到该字符串的末尾。
  • public char charAt (int index) :返回指定索引处的 char值。
  • public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
  • public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。
  • public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。
public class Test {

    public static void main(String[] args) {
        // 创建字符串对象
        String s1 = "helloworld";

        //length() 获取字符串的长度,其实也就是字符个数
        System.out.println(s1.length());

        // concat(String str):将将指定的字符串连接到该字符串的末尾
        System.out.println(s1.concat("+java"));

        //charAt(int index):获取指定索引处的字符
        System.out.println(s1.charAt(0));
        System.out.println(s1.charAt(2));

        //indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
        System.out.println(s1.indexOf("h"));
        System.out.println(s1.indexOf("i"));

        //substring(int start):从start开始截取字符串到字符串结尾
        System.out.println(s1.substring(0));
        System.out.println(s1.substring(5));

        //substring(int start,int end):从start到end截取字符串。含start,不含end。
        System.out.println(s1.substring(1,4));
        System.out.println(s1.substring(0,6));

        System.out.println(s1);

    }
}

转换功能的方法

  • public char[] toCharArray () :将此字符串转换为新的字符数组。
  • public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
  • public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。
public class Test {
    public static void main(String[] args) {
        //创建字符串对象
        String s = "abcde";

        // char[] toCharArray():把字符串转换为字符数组
        char[] chs = s.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            System.out.println(chs[i]);
        }

        //byte[] getBytes ():把字符串转换为字节数组
        byte[] bytes = s.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        // 替换字母
        String str = s.replace("a","g");
        System.out.println(s);
        System.out.println(str);

    }
}

分割功能的方法

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

public class Test {
    public static void main(String[] args) {
        //创建字符串对象
        String s = "abcde";
        String[] strArray = s.split("c");
        for (int i = 0; i < strArray.length; i++) {
            System.out.println(strArray[i]);
        }
    }
}