使用到的场景 string 的系列问题刷题遇见
1. 创建String的过程(拙见)
String str = “abc”;
相当于
char data[] = {‘a’, ‘b’, ‘c’};
String str = new String(data);
看源码中的 value 的创建时候就是一个字符数组.
创建的区域是在jvm的共享堆中(JAVA中有个字符串池,它存储在堆(heap)中,可共享)
String s1 = "Apple";
String s2 = new String("Apple");
String s3 = "Apple";
String s4 = new String("Apple");
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s2==s4);
输出: false ture false
直接创建的相等的形式流程:
先在堆中查找是否存在 Apple , 如果不存在那么创建一个 Apple 在堆中, 让 s1 指向 Apple的地址, 那么在后面使用同样的方式创建 s3 的时候直接判断堆中是否存在, 如果存在则直接 让s3 指向Apple 的地址, 所以 s1 == s3 输出为true
对于 new 出来的String而言他的每一次 new 都是创建一个新的对象的过程, 所以 输出的 s2 == s4 为 false
2. char charAt(int index)
char charAt(int index)
返回指定索引处的 char 值。
// 源码如下 , 直接返回字符数组中的对应的位置的字符
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
3. String replace(char oldChar, char newChar)
String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
// 源码如下 ,
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
// 找到原字符数组中的的oldChar 第一次出现的位置
// 或者说是判断, 原字符数组中是否存在oldChar字符
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
// 保存第一次出现oldChar前的字符
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
// 从第一次出现的位置开始进行遍历, 进行数组对应位置字符的替换
while (i < len) {
char c = val[i];
// 如果old数组中对应的字符等于传入的oldChar, 则这个位置就更改为newChar
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
4. String substring(int beginIndex, int endIndex)
String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
// 源码如下
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);
}
// (beginIndex == 0) && (endIndex == value.length)
// 作用是判断子串开始位置和结束位置的长度是否与原字符串的长度相同, 如果相同则直接返回原字符串, 否则进行字符串的拷贝, 返回截取的串
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
// new String(value, beginIndex, subLen); 的源码
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);
}
// Arrays.copyOfRange(value, offset, offset+count); 的源码
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
5. String trim()
String trim()
返回字符串的副本,忽略前导空白和尾部空白。
// 源码如下,
public String trim() {
int len = value.length;
int st = 0;
char[] val = value; /* avoid getfield opcode */
// 去除前面的空格
while ((st < len) && (val[st] <= ' ')) {
st++;
}
// 去除后面的空格
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
参考文档,官方JDK = https://tool.oschina.net/apidocs/apidoc?api=jdk-zh