Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
Look at the source code, we can see that the bottom String is modified with final, and worth storage is stored in character array
- The charAt method returns the character value at the specified index. Indexes range from 0 to length()
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
Copy the code
- GetChars copies the characters from the string into the target character array.
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);
}
Copy the code
- Another equals article is available if needed
- CompareTo compares two strings in lexicographical order. If the String object precedes the String argument in lexicographical order, the result is a negative integer. If String follows the argument String in lexicographical order, the result is a positive integer. If two strings are equal, the result is zero; When equals(Object) returns true, compareTo returns exactly 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; }Copy the code
- Hashcode returns the hashcode for this string
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;
}
Copy the code
- IndexOf returns the indexOf the first occurrence of the specified character in the string, and the search begins at the specified index.
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); }}Copy the code
- LastIndexOf returns the index of the last occurrence of the specified character in the string
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);
}
}
Copy the code
- 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);
}
Copy the code
- Concat concates the specified string to the end of the 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);
str.getChars(buf, len);
return new String(buf, true);
}
Copy the code
- Replace returns a string that replaces all occurrences of oldChar in the string with 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; }Copy the code
- Contains Returns true if and only if the string contains the specified sequence of character values
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
Copy the code
- Trim returns the string for this string, with leading and trailing whitespace removed.
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;
}
Copy the code
- ToUpperCase (); ToLowerCase (); String case conversion
In the process of reading the String source code, there are many overloaded methods (the method name is the same, but the argument list is different), I have shared some common methods and part of the source code, in fact, look at these source code, and we found that the code is similar to our ordinary code, but they are better encapsulation wow. We read more source code for us to write code will also have a lot of inspiration and help, for example, each method above, the basic will be the first to verify the passed parameters, the number passed is legal, if not legal directly throw an exception. (Previous article also shared how to customize their own exceptions how to throw exceptions if interested can go to see