17.Java_API String


一、String 类概述

1、概述

java.lang.String类代表字符串。Java程序中所有的字符串文字(例如”abc”)都可以被看作是此类的实例。

String中包含用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻译为大写或者小写的所有字符的字符串的副本。

2、特点

  • 字符串不变:字符串的值在创建后不能被更改
String s1 = "abc";
s1 += "d";
System.out.println(s1); // "abcd"
// 内存中有"abc", "abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"
  • 因为String对象是不可变的,所以它们可以被共享
String s1 = "abc";
String s2 = "abc";
// 内存中只有一个"abc"对象被创建,同时被s1和s2共享
  • “abc”等效于 char[] data = {'a', 'b', 'c'}
String str = "abc";

char[] data = {'a', 'b', 'c'};
String str = new String(data);
// String 底层是靠字符数组实现的

二、使用步骤

1、查看类

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

2、查看构造方法

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

// 通过字符数组来构造
char[] chars = {'a', 'b', 'c'};
String str2 = new String(chars);

// 通过字节数组来构造
byte[] bytes = {97, 98, 99};
String str3 = new String(bytes);

3、常用方法

(1)、判断功能的方法

  • public boolean equals(Object anObject):将此字符串与指定对象进行比较
  • public boolean equalsIsIgnoreCase(String anotherString): 将此字符串与指定对象进行比较,忽略大小写
public class String_Demo01{
    public static void main(String[] args){
        // 创建字符串对象
        String s1 = "hello";
        String s2 = "helo";
        String s3 = "Hello";
        
        // equals
        System.out.println(s1.equals(s2)); // true
        System.out.println(s1.equals(s3)); // false
        System.out.println("========");

        // equalsIngoreCase
        System.out.println(s1.equalsIgnoreCase(s2));// true
        System.out.println(s1.equalsIgnoreCase(s3));//true
    }
}

(2)、获取功能的方法

  • 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节区字符串,含beginIndx,不含endIndex
public class Demo02StringFunc {
    public static void main(String[] args) {
        // 创建字符串对象
        String s = "helloworld";

        // int length()
        System.out.println(s.length());
        System.out.println("========");

        // String concat(String str)
        String s2 = s.concat(", Java");
        System.out.println(s2);
        System.out.println("========");

        // char charAt(int index)
        System.out.println(s.charAt(0));
        System.out.println(s.charAt(1));
        System.out.println("========");

        // int indexOf(String str)
        System.out.println(s.indexOf("l"));
        System.out.println(s.indexOf("owo"));
        System.out.println("========");

        // String substring(int beginIndex)
        System.out.println(s.substring(0));
        System.out.println(s.substring(4));
        System.out.println("========");

        // String substring(int beginIndex, int endIndex)
        System.out.println(s.substring(0, s.length()));
        System.out.println(s.substring(3, 8));
    }
}

(3)、转换功能的方法

  • public char[] toCharArray(): 将此字符串转换为新的字符数组
  • public byte[] getBytes(): 使用平台的默认字符集将该String编码转换为新的字节数组
  • public String replace(CharSequence target, CharSequence replacement): 将于target匹配的字符串使用replacement字符串替换
public class Demo03StringTrans {
    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]);
        }
        System.out.println("========");

        // byte[] getBytes()
        byte[] bytes = s.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
        System.out.println("========");

        // 替换字母it为IT
        String str = "itcast";
        String replace = str.replace("it", "IT");
        System.out.println(replace);
    }
}

CharSequence 是一个接口,也是一种引用类型,作为参数类型,可以把String对象传递到方法中

(4)、分割功能

  • public String[] split(String regex): 将此字符串按照给定的regex拆分为字符串数组
public class Demo04StringSplit {
    public static void main(String[] args) {
        // 创建字符出对象
        String s = "aa,bb,cc";
        String[] strArray = s.split(",");
        System.out.println(strArray.length);
        for (int i = 0; i < strArray.length; i++) {
            System.out.println(strArray[i]);
        }
    }
}

Author: Wolfwotz
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Wolfwotz !
  TOC