【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)
https://blog.youkuaiyun.com/m0_69908381/article/details/144788579
出自【进步*于辰的博客】
文章目录
- 1、概述
- 2、构造方法摘要
-
- 2.1 null
- 2.2 byte[] bytes
- 2.3 ~~byte[] ascii, int hibyte~~
- 2.4 byte[] bytes, int offset, int length
- 2.5 ~~byte[] ascii, int hibyte, int offset, int count~~
- 2.6 byte[] bytes, int offset, int length, String charsetName
- 2.7 byte[] bytes, String charsetName
- 2.8 char[] value
- 2.9 char[] value, int offset, int count
- 2.10 int[] codePoints, int offset, int count
- 2.11 String original
- 2.12 StringBuffer buffer
- 2.13 StringBuilder builder
- 3、方法摘要
-
- 3.1 char charAt(int index)
- 3.2 int codePointAt(int index)
- 3.3 int codePointBefore(int index)
- 3.4 int codePointCount(int beginIndex, int endIndex)
- 3.5 int compareTo(String anotherString)
- 3.6 int compareToIgnoreCase(String str)
- 3.7 String concat(String str)
- 3.8 boolean contains(CharSequence s)
- 3.9 boolean contentEquals(CharSequence cs)
- 3.10 boolean contentEquals(StringBuffer sb)
- 3.11 static String copyValueOf(char[] data)
- 3.12 static String copyValueOf(char[] data, int offset, int count)
- 3.13 boolean endsWith(String suffix)
- 3.14 boolean equals(Object anObject)
- 3.15 boolean equalsIgnoreCase(String anotherString)
- 3.16 static String format(Locale l, String format, Object... args)
- 3.17 static String format(String format, Object... args)
- 3.18 byte[] getBytes()
- 3.19 ~~void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)~~
- 3.20 byte[] getBytes(String charsetName)
- 3.21 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- 3.22 int hashCode()
- 3.23 int indexOf(int ch)
- 3.24 int indexOf(int ch, int fromIndex)
- 3.25 int indexOf(String str)
- 3.26 int indexOf(String str, int fromIndex)
- 3.27 native String intern()
- 3.28 int lastIndexOf(int ch)
- 3.29 int lastIndexOf(int ch, int fromIndex)
- 3.30 int lastIndexOf(String str)
- 3.31 int lastIndexOf(String str, int fromIndex)
- 3.32 int length()
- 3.33 boolean matches(String regex)
- 3.34 int offsetByCodePoints(int index, int codePointOffset)
- 3.35 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- 3.36 boolean regionMatches(int toffset, String other, int ooffset, int len)
- 3.37 String replace(char oldChar, char newChar)
- 3.38 String replace(CharSequence target, CharSequence replacement)
- 3.39 String replaceAll(String regex, String replacement)
- 3.40 String replaceFirst(String regex, String replacement)
- 3.41 String[] split(String regex)
- 3.42 String[] split(String regex, int limit)
- 3.43 boolean startsWith(String prefix)
- 3.44 boolean startsWith(String prefix, int toffset)
- 3.46 String substring(int beginIndex)
- 3.47 String substring(int beginIndex, int endIndex)
- 3.48 char[] toCharArray()
- 3.49 String toLowerCase()
- 3.50 String toLowerCase(Locale locale)
- 3.51 String toString()
- 3.52 String toUpperCase()
- 3.53 String toUpperCase(Locale locale)
- 3.54 boolean isEmpty()
- 3.55 String trim()
- 3.56 static String valueOf(boolean b)
- 3.57 static String valueOf(char c)
- 3 58 static String valueOf(char[] data)
- 3.59 static String valueOf(char[] data, int offset, int count)
- 3.60 static String valueOf(double d)
- 3.61 static String valueOf(float f)
- 3.62 static String valueOf(int i)
- 3.63 static String valueOf(long l)
- 3.64 static String valueOf(Object obj)
- 4、构造方法摘要(不开放)
- 5、方法摘要(不开放)
-
- 5.1 void getChars(char dst[], int dstBegin)
- 5.2 static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex)
- 5.3 static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex)
- 5.4 private int indexOfSupplementary(int ch, int fromIndex)
- 5.5 private int lastIndexOfSupplementary(int ch, int fromIndex)
- 5.6 private boolean nonSyncContentEquals(AbstractStringBuilder sb)
- 6、字段摘要
- 最后
1、概述
继承关系:
- java.lang.Object
- java.lang.String
所有已实现的接口:
Serializable、CharSequence、Comparable<String>
public final class String extends Object implements Serializable, Comparable<String>, CharSequence
String 类代表字符串。Java 程序中的所有 字符串字面值 \color{green}{字符串字面值} 字符串字面值(如"abc"
)都作为此类的实例来实现。
字符串是常量;它们的值在创建之后不能改变。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享它们。例如:
String str = "abc";
等效于:
char data[] = {
'a', 'b', 'c'};
String str = new String(data);
下面给出了一些如何使用字符串的更多例子:
sout "abc";
String cde = "cde";
sout "abc" + cde;
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);
String 类包括的方法有:检查序列的单个字符;比较字符串;搜索字符串;提取子字符串;创建字符串副本,在该副本中,所有的字符都被转换为大写或小写形式。大小写映射基于 Character 类指定的 Unicode Standard 版本。
Java 语言提供对 字符串串联符号 \color{blue}{字符串串联符号} 字符串串联符号(“+
”)和其他对象到字符串的转换的特殊支持。字符串串联是通过 StringBuilder(或 StringBuffer)类及其 append()
实现的。字符串转换是通过 toString()
实现的,该方法由 Object 类定义,并可被 Java 中所有类继承。有关字符串串联和转换的更多信息,请参阅 Gosling、Joy 和 Steele 合著的 《The Java Language Specification》。
注意: \color{red}{注意:} 注意:除非另行说明,否则将 null 参数传递给此类中的构造方法或方法都会抛出 NullPointerException。
String 表示一个UTF-16
格式的字符串,其中的增补字符由 代理项对 \color{brown}{代理项对} 代理项对表示(有关详细信息,请参阅字符类中的 Unicode 字符表示形式)。索引值是指 char 代码单元,因此增补字符在 String 中占用两个位置 。
注: \color{red}{注:} 注:String 类提供处理 Unicode 代码点和 Unicode 代码单元的方法。 (关于代码点与代码单元的说明,见字符类)
从以下版本开始:
JDK1.0
另请参见:
Object.toString()、StringBuffer、StringBuilder、Charset、序列化表格
2、构造方法摘要
属性说明:
private final char value[];
private int hash; // 默认值: 0
2.1 null
初始化一个新创建的 String 对象,它表示一个空字符序列。
public String() {
this.value = "".value;
}
2.2 byte[] bytes
构造一个新的 String,方法是使用平台的默认字符集解码字节的指定数组。
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
调用第4项,故 offset 为0
,length 为bytes.length
。
2.3 byte[] ascii, int hibyte
已过时。 该方法无法将字节正确转换为字符。从 JDK 1.1 起,完成该转换的首选方法是通过 String 构造方法,该方法接受一个字符集名称或使用平台的默认字符集。
2.4 byte[] bytes, int offset, int length
构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。即取 bytes 中从offset
开始、length
长度内的byte 序列构建字符串。
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
后续补充。
2.5 byte[] ascii, int hibyte, int offset, int count
已过时。 该方法无法将字节正确转换为字符。从 JDK 1.1 开始,完成该转换的首选方法是通过 String 构造方法,它接受一个字符集名称,或者使用平台默认的字符集。
2.6 byte[] bytes, int offset, int length, String charsetName
构造一个新的 String,方法是使用指定的字符集解码字节的指定子数组。
public String(byte bytes[], int offset, int length, String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null)
throw new NullPointerException("charsetName");
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(charsetName, bytes, offset, length);
}
后续补充。
2.7 byte[] bytes, String charsetName
构造一个新的 String,方法是使用指定的字符集解码指定的字节数组。
public String(byte bytes[], String charsetName)
throws UnsupportedEncodingException {
this(bytes, 0, bytes.length, charsetName);
}
2.8 char[] value
分配一个新的 String,它表示当前字符数组参数中包含的字符序列。
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
将字符序列value
所有字符取出,赋值于当前字符序列。
2.9 char[] value, int offset, int count
分配一个新的 String,它包含来自该字符数组参数的一个子数组的字符。
2.10 int[] codePoints, int offset, int count
分配一个新的 String,它包含该 Unicode 代码点数组参数的一个子数组的字符。即先将int[]
转为字符序列,再构造新 String。
2.11 String original
初始化一个新创建的 String 对象,表示一个与该参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的一个副本。
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
2.12 StringBuffer buffer
分配一个新的字符串,它包含当前包含在字符串缓冲区参数中的字符序列。
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
2.13 StringBuilder builder
分配一个新的字符串,它包含当前包含在字符串生成器参数中的字符序列。
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
可见,此方法与上一项的底层都调用copyOf()
。
将字符串缓冲区/生成器中的所有字符取出,赋值于当前字符序列。
注意: \color{red}{注意:} 注意:上一项使用了同步锁synchronized
,故线程安全,而此项不是。
3、方法摘要
3.1 char charAt(int index)
返回指定索引处的 char 值。
public char charAt(int index) {
// value是此字符串的字符序列。类型是char[],末尾元素索引为value.length - 1
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
3.2 int codePointAt(int index)
返回指定索引处的字符(Unicode 代码点),
public int codePointAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, value.length);
}
3.3 int codePointBefore(int index)
返回指定索引之前的字符(Unicode 代码点),
public int codePointBefore(int index) {
int i = index - 1;
if ((i < 0) || (i >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointBeforeImpl(value, index, 0);
}
3.4 int codePointCount(int beginIndex, int endIndex)
返回此 String 的指定文本范围中的 Unicode 代码点数。
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);
}
3.5 int compareTo(String anotherString)
按字典顺序比较两个字符串。
示例:
"csdn".compareTo("csXn");// 12-----------A
"csdn".compareTo("cs");// 2--------------b
代码说明:
- 代码A。
"csdn"
与"csXn"
长度相同,因此以"csdn"
的字符序列作为遍历限制。它们的第一个不相同字符的索引是2,即"d"
和"X"
。其中,"d"
的 A S C L L 码 \color{green}{ASCLL码} ASCLL码是100,而"X"
是88,故结果是12; - 代码B。
"csdn"
与"cs"
长度不相同,因此以"cs"
的字符序列作为遍历限制。在"cs"
的字符序列内,2个字符串的字符序列相同,故直接返回2字符串的长度差,得2。
3.6 int compareToIgnoreCase(String str)
不考虑大小写,按字典顺序比较两个字符串,
public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
后续解析。
3.7 String concat(String str)
将指定字符串联到此字符串的结尾。
public String concat(String str) {
if (str.isEmpty()) {
return this;
}
int len = value.length;
int otherLen = str.length();
char buf[] = Arrays.copyOf(value, len + otherLen);// 扩容,扩容部分都是空字符,以便存储追加字符
// 将buf的字符序列追加到当前字符序列末尾,末尾后一位的索引是 value.length
str.getChars(buf, len);
return new String(buf, true);
}
3.8 boolean contains(CharSequence s)
当且仅当此字符串包含 char 值的指定序列时,才返回 true。
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
3.9 boolean contentEquals(CharSequence cs)
当且仅当此 String 表示与指定序列相同的 char 值时,才返回 true。
/**
* 1、CharSequence 接口的实现类有:此类、StringBuilder、StringBuffer、CharBuffer。
* 2、此类底层逻辑类似 equals()。
*/
public boolean contentEquals(CharSequence cs) {
// 若 cs 是 StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) {
if (cs instanceof StringBuffer) {
synchronized(cs) {
return nonSyncContentEquals((AbstractStringBuilder)cs);// 同 equals()
}
} else {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
// 若 cs 是 String
if (cs instanceof String) {
return equals(cs);
}
// 若 cs 是“通用的” CharSequence 子类
char v1[] = value;
int n = v1.length;
if (n != cs.length()) {
return false;
}
for (int i = 0; i < n; i++) {
if (v1[i] != cs.charAt(i)) {
return false;
}
}
return true;
}
3.10 boolean contentEquals(StringBuffer sb)
当且仅当此 String 表示与指定的 StringBuffer 相同的字符序列时,才返回 true。
public boolean contentEquals(StringBuffer sb) {
return contentEquals((CharSequence)sb);
}
3.11 static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的字符串。
public static String copyValueOf(char data[]) {
return new String(data);
}
调用第2.8项。
3.12 static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的字符串。
public static String copyValueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
调用第2.9项。
3.13 boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
调用第44项,第44项阐明,测试此字符串是否以指定前缀开始,对应toffset
(开始索引)为0
,即第43项。而若toffset
为value.length - suffix.value.length
,自然是:测试此字符串是否以指定后缀结束。
3.14 boolean equals(Object anObject)
比较此字符串与指定的对象。
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) {
// 若2个字符串长度不相等,自然不相等,直接返回 false
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;
}
3.15 boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 进行比较,不考虑大小写。
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
后续解析。
3.16 static String format(Locale l, String format, Object… args)
使用指定的语言环境(语种)、格式字符串和参数返回一个格式化字符串。
public static String format