From 5f005bb94912664ab86e0a41b41e4f59b4ba8c0e Mon Sep 17 00:00:00 2001 From: AmyliaY <471816751@qq.com> Date: Fri, 19 Jun 2020 20:38:32 +0800 Subject: [PATCH] =?UTF-8?q?JDK1.8=E4=B9=8BString=E6=BA=90=E7=A0=81?= =?UTF-8?q?=E8=B5=8F=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/JDK/String.md | 203 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 202 insertions(+), 1 deletion(-) diff --git a/docs/JDK/String.md b/docs/JDK/String.md index fcb5dbe..8772e1a 100644 --- a/docs/JDK/String.md +++ b/docs/JDK/String.md @@ -1 +1,202 @@ -努力编写中... \ No newline at end of file +String的源码大家应该都能看懂,这里就不一一分析咯,重点讲一下equals()和hashcode()方法,然后看一下String类常用方法的实现,就当一起温习一下咯。 +```java +public final class String + implements java.io.Serializable, Comparable, CharSequence { + + /** 保存String的字节数组 */ + private final char value[]; + + /** 缓存这个String的hash值 */ + private int hash; // Default to 0 + + /** use serialVersionUID from JDK 1.0.2 for interoperability */ + private static final long serialVersionUID = -6849794470754667710L; + + /** + * 1、Object的 hashCode()返回该对象的内存地址编号,而equals()比较的是内存地址是否相等; + * 2、需要注意的是当equals()方法被重写时,hashCode()也要被重写; + * 3、按照一般hashCode()方法的实现来说,equals()相等的两个对象,hashcode()必须保持相等; + * equals()不相等的两个对象,hashcode()未必不相等 + * 4、一个类如果要作为 HashMap 的 key,必须重写equals()和hashCode()方法 + */ + public boolean equals(Object anObject) { + if (this == anObject) { + return true; + } + if (anObject instanceof String) { + String anotherString = (String)anObject; + int n = value.length; + if (n == anotherString.value.length) { + char v1[] = value; + char v2[] = anotherString.value; + int i = 0; + while (n-- != 0) { + if (v1[i] != v2[i]) + return false; + i++; + } + return true; + } + } + return false; + } + + 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; + } + + /** + * 指定下标的char + */ + public char charAt(int index) { + if ((index < 0) || (index >= value.length)) { + throw new StringIndexOutOfBoundsException(index); + } + return value[index]; + } + + /** + * 是否以 prefix 为前缀 + */ + public boolean startsWith(String prefix) { + return startsWith(prefix, 0); + } + + /** + * 是否以 suffix 为后缀 + */ + public boolean endsWith(String suffix) { + return startsWith(suffix, value.length - suffix.value.length); + } + + /** + * 该String对象 是否满足 regex正则表达式 + */ + public boolean matches(String regex) { + return Pattern.matches(regex, this); + } + + /** + * 字符替换 + */ + 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; + } + + /** + * 子串替换 + */ + public String replaceAll(String regex, String replacement) { + return Pattern.compile(regex).matcher(this).replaceAll(replacement); + } + + /** + * 子串替换,只替换第一个 + */ + public String replaceFirst(String regex, String replacement) { + return Pattern.compile(regex).matcher(this).replaceFirst(replacement); + } + + /** + * 按 regex 切割成多个子串 + */ + public String[] split(String regex) { + return split(regex, 0); + } + + /** + * 剪切指定范围的字符串 + */ + 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); + } + + /** + * 获取该String 对应的 char[] + */ + public char[] toCharArray() { + // Cannot use Arrays.copyOf because of class initialization order issues + char result[] = new char[value.length]; + System.arraycopy(value, 0, result, 0, value.length); + return result; + } + + /** + * 大小写转换 + */ + public String toLowerCase() { + return toLowerCase(Locale.getDefault()); + } + public String toUpperCase() { + return toUpperCase(Locale.getDefault()); + } + + /** + * str在本String对象中第一次出现的下标 + */ + public int indexOf(String str) { + return indexOf(str, 0); + } + + /** + * str在本String对象中最后一次出现的下标 + */ + public int lastIndexOf(String str) { + return lastIndexOf(str, value.length); + } +} +``` \ No newline at end of file