
正文
jdk之String
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
学习几个常用的String方法
1、concat
/**
* Concatenates the specified string to the end of this string.
连接指定的字符串到该字符串后面
*/
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);
//封装的getChars方法。对buf字节组的第len的位置。将指定字符串加到buf数组中
str.getChars(buf, len);
return new String(buf, true);
}
2、charAt
/**
* Returns the {@code char} value at the
* specified index.
返回指定索引的值
*/
public char charAt(int index) {
//判断当前索引是否小于0或者当前字符串的长度。是则抛异常
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
//返回字节数组指定位置的值
return value[index];
}
3、contains







