JDK1.5加入的新特性。java.lang包底下的类,被final所修饰,value也是被final修饰,本质是一个不可变的字符数组。
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
}
常用方法函数
equals
比较两个字符串的内容
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); // true
System.out.println(s1.equals(s2)); // true
- s1和s2的引用地址都指向一个同一个字符串常量。所以==比较引用地址是true
String s3 = new String("abc");
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s3)); // true
- s3是通过new出来的,在堆上面分配新内存地址。所以==比较是false
String s4 = "hello world";
String s5 = "hello" + " world";
System.out.println(s4 == s5); // true
System.out.println(s4.equals(s5)); // true
- s5 是由 hello和world组成新的字符串,会先去字符串常量池里查找是否有对应的字符串,没有则创建hello world字符串。所有s5和s4指向同一个引用地址。==返回的结果为true
String s6 = "hello world oh!";
String s7 = s5 + "oh!";
System.out.println(s6 == s7); // fasle
System.out.println(s6.equals(s7)); // true
- s5不是常量,是引用对象,所以s7会分配新的引用地址。==返回结果为false
isEmpty
public boolean isEmpty() {
return value.length == 0;
}
- 判断字符串长度是否为0
length
public int length() {
return value.length;
}
- 返回字符串的字符长度,实际返回的是字符数组的长度
indexOf
String s1 = "abc";
String s2 = "bc";
String s3 = "ac";
System.out.println(s1.indexOf(s2));// 返回 1
System.out.println(s1.indexOf(s3));// 返回 -1
- 判断某个字符串或字符是否被字符串包含,存在返回字符或字符串最开始出现的下角标索引,不存在则返回-1
substring
截取字符串
String s = "abcde";
System.out.println(s.substring(2)); // cde
System.out.println(s.substring(1,3)); // bc
- substring(int beginIndex) 从某个字符索引开始向后截取
- substring(int beginIndex, int endIndex) 截取begin和end字符串字符索引的字符串,不包括end的索引字符
valueOf
int i = 10;
Integer i2 = 20;
float f = 12.5f;
System.out.println(String.valueOf(i));
System.out.println(String.valueOf(i2));
System.out.println(String.valueOf(f));
- 将不同类型的数据装换成字符串,包装类会返回值
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
public class Test01 {
private int age = 5;
private String name = "张三";
@Override
public String toString() {
return "{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public static void main(String[] args) {
Test01 test01 = new Test01();
System.out.println(String.valueOf(test01)); //结果:{age=5, name='张三'}
}
}
- 如果是Object对象 则会返回对象的toString()方法
split
String s = "a,b,c,d";
String[] arr = s.split(",");
System.out.println(Arrays.toString(arr)); //结果:[a, b, c, d]
- 字符串切割,并返回一个字符数组
replace / replaceAll
String s = "abbccd";
System.out.println(s.replace("b", "B")); // 结果:aBBccd
System.out.println(s.replaceAll("c", "C"));// 结果:abbCCd
- String.replace() 和 String.replaceAll() 调用的方法是一样的,都是Matcher.replaceAll() 方法
- replaceAll可以使用正则表达式
String相关类
String的是final修饰不可变类,StringBuffer和StringBuilder是可变字符串对象。
StringBuffer
线程安全的。类的方法基本有synchronized关键字
@Override
public synchronized int length() {
return count;
}
@Override
public synchronized int capacity() {
return value.length;
}
@Override
public synchronized void ensureCapacity(int minimumCapacity) {
if (minimumCapacity > value.length) {
expandCapacity(minimumCapacity);
}
}
StringBuilder
线程不安全的。