String类基本介绍
1、在java.lang包下,可以直接使用,无需导包
package java.lang;
2、被final修饰,不能被继承,没有子类
public final class String
3、String底层是一个char类型的数组
private final char value[];
常用方法
空参构造
public String() {
this.value = "".value;
}
有参构造
String类提供了大量的有参勾走方法,不管是空参构造还是有参构造,本质都是给底层的字符数组进行赋值。
字符串参数:
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
字符数组:
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
截取指定的字符数组创建字符串:
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
以及还有参数为int数组 byte数组的构造器,后续补充。
判断两字符串内容是否一样:equals方法
String s6 = new String("abc");
String s7 = new String("abc");
System.out.println(s6.equals(s7));
结果:true
String重写了equals方法,比较的是对象的内容:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
compareTo方法:
String类实现了Comparable接口,重写了compareTo方法,
如果仅仅判断两个字符串内容是否完全一致,则此方法和equals方法没有区别,此方法返回 0 ,equals方法 true
但是如果是两个不同的字符串,conpareTo方法在进行字符串内容比较的同时,根据不同调用者返回值的不同,间接判断两个字符串的先后顺序:
举例:
public static void main(String[] args) {
String str = "A";
String str1 = "a";
System.out.println("str=" + str);
System.out.println("str1=" + str1);
System.out.println("str.compareTo(str1)的结果是:" + str.compareTo(str1));
System.out.println("str1.compareTo(str)的结果是:" + str1.compareTo(str));
System.out.println("str1.compareTo('a')的结果是:" + str1.compareTo("a"));
}
上面结果
str = A
str1 = a
str.compareTo(str1)的结果是:-32
str1.compareTo(str)的结果是:32
str1.compareTo('a')的结果是:0
compareTo重写源码:
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
源码解析:
字符串截取:
字符串因为是字符数组,字符下标从0开始。
方法一:从指定下标处(包括指定下标),截取到字符串结尾
举例:
String str = "abcdef";
String sub = str.substring(2);
System.out.println(sub);
结果:cdef
源码:
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
方法一:截取指定长度字符,包头不包尾
举例:
String str = "abcdef";
String sub = str.substring(2,4);
System.out.println(sub);
结果:cd
源码:
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
字符串拼接:
举例:
String str = "abc";
String concatStr = str.concat("def");
System.out.println(concatStr);
结果:abcdef
源码:
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
字符串中字符替换:
方法一:将所有指定字符进行替换:
举例:
String str = "aac";
String replaceStr = str.replace("a", "0");
System.out.println(replaceStr);
结果:00c
字符串根据指定字符分割成数组:
举例:
String s12 = "a-b-c-d-e-f";
String[] strs = s12.split("-");
System.out.println(Arrays.toString(strs));
结果:[a, b, c, d, e, f]