String部分常用的方法
查看源码可知,String底层是用final修饰的,而值得存储时用字符数组存储得
charAt方法 返回指定索引处的字符值。索引范围从0到length()
public char charAt(int index) { if ((index < 0) || (index >= value.length)) { throw new StringIndexOutOfBoundsException(index); } return value[index]; }复制代码
getChars 将该字符串中的字符复制到目标字符数组中。
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); }复制代码
equals 另一篇文章有 如有需要可以点击
compareTo 按字典顺序比较两个字符串 如果String对象按字典顺序排在参数String之前,则结果为负整数。如果String对象按字典顺序跟随参数String,则结果为正整数。如果两个字符串相等,则结果为零;当equals(Object)方法返回true时,compareTo恰好返回0
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; }复制代码
hashcode 返回此字符串的哈希码
public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }复制代码
indexOf 返回该字符串中指定字符第一次出现的索引,并从指定索引处开始搜索。
public int indexOf(int ch, int fromIndex) { final int max = value.length; if (fromIndex < 0) { fromIndex = 0; } else if (fromIndex >= max) { // Note: fromIndex might be near -1>>>1. return -1; } if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { // handle most cases here (ch is a BMP code point or a // negative value (invalid code point)) final char[] value = this.value; for (int i = fromIndex; i < max; i++) { if (value[i] == ch) { return i; } } return -1; } else { return indexOfSupplementary(ch, fromIndex); } }复制代码
lastIndexOf 返回该字符串中指定字符最后一次出现的索引
public int lastIndexOf(int ch, int fromIndex) { if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { // handle most cases here (ch is a BMP code point or a // negative value (invalid code point)) final char[] value = this.value; int i = Math.min(fromIndex, value.length - 1); for (; i >= 0; i--) { if (value[i] == ch) { return i; } } return -1; } else { return lastIndexOfSupplementary(ch, fromIndex); } }复制代码
substring
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); } 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); }复制代码
concat 将指定的字符串连接到该字符串的末尾
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); }复制代码
replace 返回一个字符串,将该字符串中出现的所有oldChar替换为newChar
public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ while (++i < len) { if (val[i] == oldChar) { break; } } if (i < len) { char buf[] = new char[len]; for (int j = 0; j < i; j++) { buf[j] = val[j]; } while (i < len) { char c = val[i]; buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(buf, true); } } return this; }复制代码
contains 当且仅当该字符串包含指定的字符值序列时返回true
public boolean contains(CharSequence s) { return indexOf(s.toString()) > -1; }复制代码
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; }复制代码
toUpperCase(); toLowerCase() ;字符串大小写的转换
在读String源码得过程中,有许多重载方法(方法名相同,但参数列表不同),我上面分享了一些常用的方法以及部分的源码,其实看这些源码,发现和我们平常的代码差不多,只是他们的封装更好哇,我们多读读源码 对我们写代码也会有许多的启发和帮助 就例如上面每个方法,基本都会先对传入的参数进行验证,传入的数是否合法,若不合法直接抛出异常。(之前文章也分享过如何自己自定义异常如何抛出异常 若有兴趣可以去查看
作者:又菜又想玩的XXX
链接:https://juejin.cn/post/7019170122463969316