3.1 String 类
1 API概述
2 键盘录入
Scanner
3 String 的概述
lang包下无须导包 常量无法更改智能创建
4 String常见构造方法
空参 char数组 String序列
5 创建字符串对象的区别对比
new 对象 地址不同 ""创建同序列相同
6 String特点
变量串联底层是StringBuild 及append实现的
常量拼接 具有常量优化
7 字符串的比较
8 StringBuild概述
可变字符串类
char[] value;
//长度判断如果长度不够就扩容
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0) {
value = Arrays.copyOf(value,
newCapacity(minimumCapacity));
}
}
-
srcBegin -- 字符串中要复制的第一个字符的索引。
-
srcEnd -- 字符串中要复制的最后一个字符之后的索引。
-
dst -- 目标数组。
-
dstBegin -- 目标数组中的起始偏移量。
-
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
public AbstractStringBuilder append(String str) { if (str == null) return appendNull(); int len = str.length(); ensureCapacityInternal(count + len);
// 从0开始将该字符串复制到value数组count以后的位置 str.getChars(0, len, value, count); count += len; return this; }