From fc4a591d47d11a6c354db88795e0ca67cf70169e Mon Sep 17 00:00:00 2001 From: RyanSmith <875354601@qq.com> Date: Sun, 26 Jun 2016 16:38:42 +0800 Subject: [PATCH] String --- .../String.md" | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git "a/main/JAVA/\345\205\266\344\273\226\345\267\245\345\205\267\345\214\205/String.md" "b/main/JAVA/\345\205\266\344\273\226\345\267\245\345\205\267\345\214\205/String.md" index e69de29..c1020a3 100644 --- "a/main/JAVA/\345\205\266\344\273\226\345\267\245\345\205\267\345\214\205/String.md" +++ "b/main/JAVA/\345\205\266\344\273\226\345\267\245\345\205\267\345\214\205/String.md" @@ -0,0 +1,122 @@ +### 参考文献 +- [为什么String要设计成不可变的?s](http://blog.csdn.net/renfufei/article/details/16808775) +- [为什么在定义hashcode时要使用31这个数呢?](http://blog.csdn.net/steveguoshao/article/details/12576849) + +#### 一些前置的问题 + +- java语言传递参数不存在引用传递,只有值传递 +- + + + +#### String成员列表 +``` +//注意final类型,所以不能被继承 +public final class String + implements java.io.Serializable, Comparable, CharSequence { + /** The value is used for character storage. */ + private final char value[]; + + /** Cache the hash code for the string */ + private int hash; // Default to 0 + + /** use serialVersionUID from JDK 1.0.2 for interoperability */ + private static final long serialVersionUID = -6849794470754667710L; + + /** + * Class String is special cased within the Serialization Stream Protocol. + * + * A String instance is written into an ObjectOutputStream according to + * + * Object Serialization Specification, Section 6.2, "Stream Elements" + */ + private static final ObjectStreamField[] serialPersistentFields = + new ObjectStreamField[0]; + //。。。。 + } +``` +#### String的特性 +- String是不可变对象, 意思是一旦创建,那么整个对象就不可改变. 即使新手觉得String引用变了,实际上只是(指针)引用指向了另一个(新的)对象. +- String对象会缓存其HashCode **这就是为什么我们在使用map时,一般都会用string类型作为key,因为他hashcode会缓存,多次取value时效率会高一些** +- + + +#### StringBuffer与StringBuilder +StringBuffer和StringBuilder类都表示内容可以被修改的字符串。 + +StringBuilder是线程不安全的,运行效率高。 + +如果一个字符串变量是在方法里面定义,这种情况只可能有一个线程访问它,不存在不安全的因素了,则用StringBuilder。 + +如果要在类里面定义成员变量,并且这个类的实例对象会在多线程环境下使用,那么最好用StringBuffer。 + +hashcode方法: +```java + public int hashCode() { + //默认值0,所以除第一次会算一次hash其他直接用缓存 + 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 charAt(int index) | 返回指定索引处的 char 值。 +int compareTo(Object o)| 把这个字符串和另一个对象比较。 +int compareTo(String anotherString) | 按字典顺序比较两个字符串。 +int compareToIgnoreCase(String str) | 按字典顺序比较两个字符串,不考虑大小写。 +String concat(String str) | 将指定字符串连接到此字符串的结尾。 +boolean contentEquals(StringBuffer sb) | 当且仅当字符串与指定的StringButter有相同顺序的字符时候返回真。 +static String copyValueOf(char[] data) | 返回指定数组中表示该字符序列的 String。 +static String copyValueOf(char[] data, int offset, int count) | 返回指定数组中表示该字符序列的 String。 +boolean endsWith(String suffix) | 测试此字符串是否以指定的后缀结束。 +boolean equals(Object anObject) | 将此字符串与指定的对象比较。 +boolean equalsIgnoreCase(String anotherString) | 将此 String 与另一个 String 比较,不考虑大小写。 +byte[] getBytes() | 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 +byte[] getBytes(String charsetName) | 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 +void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将字符从此字符串复制到目标字符数组。 +int hashCode() | 返回此字符串的哈希码。 +int indexOf(int ch) | 返回指定字符在此字符串中第一次出现处的索引。 +int indexOf(int ch, int fromIndex) | 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 +int indexOf(String str) | 返回指定子字符串在此字符串中第一次出现处的索引。 +int indexOf(String str, int fromIndex) | 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 +String intern() | 返回字符串对象的规范化表示形式。 +int lastIndexOf(int ch) | 返回指定字符在此字符串中最后一次出现处的索引。 +int lastIndexOf(int ch, int fromIndex) | 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 +int lastIndexOf(String str) | 返回指定子字符串在此字符串中最右边出现处的索引。 +int lastIndexOf(String str, int fromIndex) | 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 +int length() | 返回此字符串的长度。 +boolean matches(String regex) | 告知此字符串是否匹配给定的正则表达式。 +boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) | 测试两个字符串区域是否相等 +boolean regionMatches(int toffset, String other, int ooffset, int len) |测试两个字符串区域是否相等。 +String replace(char oldChar, char newChar) | 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 +String replaceAll(String regex, String replacement | 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 +String replaceFirst(String regex, String replacement) | 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 +String[] split(String regex) | 根据给定正则表达式的匹配拆分此字符串。 +String[] split(String regex, int limit) | 根据匹配给定的正则表达式来拆分此字符串。 +boolean startsWith(String prefix) | 测试此字符串是否以指定的前缀开始。 +boolean startsWith(String prefix, int toffset) | 测试此字符串从指定索引开始的子字符串是否以指定前缀开始 +CharSequence subSequence(int beginIndex, int endIndex) | 返回一个新的字符序列,它是此序列的一个子序列。 +String substring(int beginIndex) | 返回一个新的字符串,它是此字符串的一个子字符串。 +String substring(int beginIndex, int endIndex) | 返回一个新字符串,它是此字符串的一个子字符串。 +char[] toCharArray() | 将此字符串转换为一个新的字符数组。 +String toLowerCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 +String toLowerCase(Locale locale) | 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。 +String toString() | 返回此对象本身(它已经是一个字符串!)。 +String toUpperCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 +String toUpperCase(Locale locale) | 使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。 +String trim() | 返回字符串的副本,忽略前导空白和尾部空白。 +46 static String valueOf(primitive data type x)|返回给定data type类型x参数的字符串表示形式。 + -- Gitee