/*
*版權所有(c)1994、2013,Oracle和/或其關聯公司。保留所有權利。
*ORACLE專有/機密。使用須遵守許可條款。
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.lang;
import java.io.ObjectStreamField;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Formatter;
import java.util.Locale;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
*{@code String}類表示字符串。全部
*Java程序中的字符串文本,例如{@code“abc”},是
*作爲此類的實例實現。
*<p>
*字符串是常量;它們的值在
*被創造出來。字符串緩衝區支持可變字符串。
*因爲字符串對象是不可變的,所以可以共享它們。例如:
*<blockquote><pre>
*String str=“abc”;
*</pre></blockquote><p>
*相當於:
*<blockquote><pre>
*char data[]={'a','b','c'};
*String str=新字符串(數據);
*</pre></blockquote><p>
*下面是一些有關如何使用字符串的更多示例:
*<blockquote><pre>
* 系統輸出打印(“abc”);
*String cde=“cde”;
* 系統輸出打印(“abc”+cde);
*字符串c=“abc”。子字符串(2,3);
*字符串d=cde.子串(1,2);
*</pre></blockquote>
*<p>
*類{@code String}包含用於檢查
*序列的單個字符,用於比較字符串,用於
*搜索字符串、提取子字符串和創建
*將所有字符轉換爲大寫或
*小寫。案例映射基於Unicode標準版本
*由{@鏈接指定java.lang.Character語言字符類。
*<p>
*Java語言爲字符串提供了特殊的支持
*連接運算符(+),用於轉換
*字符串的其他對象。實現了字符串連接
*通過{@code StringBuilder}(或{@code StringBuffer})
*類及其{@code append}方法。
*字符串轉換通過方法實現
*{@code toString},由{@code Object}和
*由Java中的所有類繼承。有關
*字符串連接和轉換,請參見Gosling、Joy和Steele,
*<i>Java語言規範</i>。
*
*<p>除非另有說明,否則將<tt>null</tt>參數傳遞給構造函數
*或類中的方法將導致{@link NullPointerException}
*扔了。
*
*<p>A{@code String}表示UTF-16格式的字符串
*其中,<em>補充字符</em>由<em>替代項表示
*對</em>(請參見<a ref='字符.html#unicode“>Unicode碼
*的{@code Character}類中的字符表示</a>
*更多信息)。
*索引值引用{@code char}代碼單元,因此
*字符在{@code String}中使用兩個位置。
*<p>類{@code String}提供處理
*Unicode代碼點(即字符),以及
*處理Unicode代碼單元(即{@code char}值)。
*
*@作者李博因頓
*@作者亞瑟·範霍夫
*@作者馬丁·布赫霍爾茨
*@作者烏爾夫·齊比斯
*@看java.lang.ObjectétoString()
*@看java.lang.StringBuffer語言
*@看java.lang.StringBuilder語言
*@看java.nio.charset文件.字符集
*@JDK1.0之後
*/
public final class String
implements java.io.Serializable, Comparable<String>, 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;
/**
*類字符串是序列化流協議中的特殊大小寫。
*
*字符串實例根據
*<a href={@docRoot}/。/platform/serialization/spec/輸出.html">
*對象序列化規範,第6.2節,“流元素”</a>
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
/**
*初始化新創建的{@code String}對象,使其表示
*一個空字符序列。注意,這個構造函數的用法是
*不需要,因爲字符串是不可變的。
*/
public String() {
this.value = "".value;
}
/**
*初始化新創建的{@code String}對象,使其表示
*與參數相同的字符序列;換句話說
*新創建的字符串是參數字符串的副本。除非
*需要{@code original}的顯式副本,使用此構造函數是
*不需要,因爲字符串是不可變的。
*
*@param原件
*{@code字符串}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
/**
*分配一個新的{@code String},以便它表示
*字符數組參數中當前包含的字符。這個
*複製字符數組的內容;隨後修改
*字符數組不影響新創建的字符串。
*
*@param值
*字符串的初始值
*/
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
/**
*分配新的{@code String},其中包含子數組中的字符
*字符數組參數的。{@code offset}參數是
*子數組的第一個字符的索引和{@code count}
*參數指定子數組的長度。的內容
*子數組被複制;字符數組的後續修改
*不影響新創建的字符串。
*
*@param值
*作爲字符源的數組
*
*@param偏移量
*初始偏移量
*
*@param計數
*長度
*
*@拋出IndexOutboundsException
*如果{@code offset}和{@code count}參數索引
*{@code value}數組邊界之外的字符
*/
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
//注意:偏移量或計數可能接近-1>>>1。
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
/**
*分配新的{@code String},其中包含子數組中的字符
*的字符.html#unicode“>Unicode代碼點</a>數組
*爭論。{@code offset}參數是第一個代碼的索引
*子數組的點和{@code count}參數指定
*子陣列的長度。子數組的內容轉換爲
*{@code char}s;對{@code int}數組的後續修改不會
*影響新創建的字符串。
*
*@param碼位
*作爲Unicode代碼點源的數組
*
*@param偏移量
*初始偏移量
*
*@param計數
*長度
*
*@拋出IllegalArgumentException
*如果在{@code中發現任何無效的Unicode代碼點
*碼位}
*
*@拋出IndexOutboundsException
*如果{@code offset}和{@code count}參數索引
*{@code codePoints}數組邊界之外的字符
*
*@從1.5開始
*/
public String(int[] codePoints, int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= codePoints.length) {
this.value = "".value;
return;
}
}
//注意:偏移量或計數可能接近-1>>>1。
if (offset > codePoints.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
final int end = offset + count;
//步驟1:計算char[]的精確大小
int n = count;
for (int i = offset; i < end; i++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
continue;
else if (Character.isValidCodePoint(c))
n++;
else throw new IllegalArgumentException(Integer.toString(c));
}
//傳遞2:分配並填寫char[]
final char[] v = new char[n];
for (int i = offset, j = 0; i < end; i++, j++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
v[j] = (char)c;
else
Character.toSurrogates(c, v, j++);
}
this.value = v;
}
/**
*分配從數組的子數組構造的新{@code String}
*8位整數值。
*
*<p>參數{@code offset}是
*子數組,{@code count}參數指定
*子陣。
*
*<p>子數組中的每個{@code byte}都轉換爲{@code char}作爲
*在上述方法中指定。
*
*@已棄用此方法無法將字節正確轉換爲字符。
*從JDK 1.1開始,首選的方法是通過
*{@code String}接受{@link的構造函數
* java.nio.charset文件.Charset}、Charset名稱或使用平臺的
*默認字符集。
*
*@param ascii碼
*要轉換爲字符的字節
*
*@param希比
*每個16位Unicode代碼單元的前8位
*
*@param偏移量
*初始偏移量
*@param計數
*長度
*
*@拋出IndexOutboundsException
*如果{@code offset}或{@code count}參數無效
*
*@見字符串(byte[],int)
*@請參閱字符串(byte[],int,int,java.lang.String語言)
*@請參閱字符串(byte[],int,int,java.nio.charset文件.字符集)
*@請參閱字符串(byte[],int,int)
*@見字符串(字節[],java.lang.String語言)
*@見字符串(字節[],java.nio.charset文件.字符集)
*@見字符串(字節[])
*/
@Deprecated
public String(byte ascii[], int hibyte, int offset, int count) {
checkBounds(ascii, offset, count);
char value[] = new char[count];
if (hibyte == 0) {
for (int i = count; i-- > 0;) {
value[i] = (char)(ascii[i + offset] & 0xff);
}
} else {
hibyte <<= 8;
for (int i = count; i-- > 0;) {
value[i] = (char)(hibyte | (ascii[i + offset] & 0xff));
}
}
this.value = value;
}
/**
*分配新的{@code String}包含從
*一個8位整數值數組。中的每個字符
*結果字符串由相應的組件構造
*字節數組中的b</i>,以便:
*
*<blockquote><pre>
*<b><i>c</i></b>=(字符)(hibyte&0xff)<;<;8)
*|(<b><i>b</i></b>&0xff)
*</pre></blockquote>
*
*@已棄用此方法無法將字節正確轉換爲
*角色。從JDK 1.1開始,首選的方法是通過
*{@code String}接受{@link的構造函數
* java.nio.charset文件.Charset}、Charset名稱或使用平臺的
*默認字符集。
*
*@param ascii碼
*要轉換爲字符的字節
*
*@param希比
*每個16位Unicode代碼單元的前8位
*
*@請參閱字符串(byte[],int,int,java.lang.String語言)
*@請參閱字符串(byte[],int,int,java.nio.charset文件.字符集)
*@請參閱字符串(byte[],int,int)
*@見字符串(字節[],java.lang.String語言)
*@見字符串(字節[],java.nio.charset文件.字符集)
*@見字符串(字節[])
*/
@Deprecated
public String(byte ascii[], int hibyte) {
this(ascii, hibyte, 0, ascii.length);
}
/* Common private utility method used to bounds check the byte array
*和字符串使用的請求偏移量和長度值(字節[],…)
*施工人員。
*/
private static void checkBounds(byte[] bytes, int offset, int length) {
if (length < 0)
throw new StringIndexOutOfBoundsException(length);
if (offset < 0)
throw new StringIndexOutOfBoundsException(offset);
if (offset > bytes.length - length)
throw new StringIndexOutOfBoundsException(offset + length);
}
/**
*通過解碼
*使用指定字符集的字節。新{@code String}的長度
*是字符集的函數,因此可能不等於長度
*子陣的。
*
*<p>給定字節無效時此構造函數的行爲
*在給定的字符集中未指定。{@鏈接
* java.nio.charset文件.CharsetDecoder}類應在更多控件時使用
*需要解碼過程。
*
*@param字節
*要解碼爲字符的字節
*
*@param偏移量
*要解碼的第一個字節的索引
*
*@param長度
*要解碼的字節數
*@param字符集名稱
*支持的{@linkplain的名稱java.nio.charset文件.字符集
*字符集}
*
*@throws不支持編碼異常
*如果不支持命名字符集
*
*@拋出IndexOutboundsException
*如果{@code offset}和{@code length}參數索引
*{@code bytes}數組界限之外的字符
*
*@從JDK1.1開始
*/
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);
}
/**
*通過解碼
*使用指定{@linkplain的字節java.nio.charset文件.Charset字符集}。
*新{@code String}的長度是字符集的函數,並且
*因此可能不等於子陣的長度。
*
*<p>此方法總是替換格式錯誤的輸入和不可映射的字符
*使用此字符集的默認替換字符串的序列。{@鏈接
* java.nio.charset文件.CharsetDecoder}類應在更多控件時使用
*需要解碼過程。
*
*@param字節
*要解碼爲字符的字節
*
*@param偏移量
*要解碼的第一個字節的索引
*
*@param長度
*要解碼的字節數
*
*@param字符集
*{@link平原java.nio.charset文件.Charset Charset}用於
*解碼{@code bytes}
*
*@拋出IndexOutboundsException
*如果{@code offset}和{@code length}參數索引
*{@code bytes}數組界限之外的字符
*
*@從1.6開始
*/
public String(byte bytes[], int offset, int length, Charset charset) {
if (charset == null)
throw new NullPointerException("charset");
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(charset, bytes, offset, length);
}
/**
*通過解碼指定的字節數組來構造新的{@code String}
*使用指定的{@linkplainjava.nio.charset文件.Charset字符集}。這個
*新{@code String}的長度是字符集的函數,因此
*不能等於字節數組的長度。
*
*<p>給定字節無效時此構造函數的行爲
*在給定的字符集中未指定。{@鏈接
* java.nio.charset文件.CharsetDecoder}類應在更多控件時使用
*需要解碼過程。
*
*@param字節
*要解碼爲字符的字節
*
*@param字符集名稱
*支持的{@linkplain的名稱java.nio.charset文件.字符集
*字符集}
*
*@throws不支持編碼異常
*如果不支持命名字符集
*
*@從JDK1.1開始
*/
public String(byte bytes[], String charsetName)
throws UnsupportedEncodingException {
this(bytes, 0, bytes.length, charsetName);
}
/**
*通過解碼指定的
*使用指定{@linkplain的字節java.nio.charset文件.Charset字符集}。
*新{@code String}的長度是字符集的函數,並且
*因此可能不等於字節數組的長度。
*
*<p>此方法總是替換格式錯誤的輸入和不可映射的字符
*使用此字符集的默認替換字符串的序列。{@鏈接
* java.nio.charset文件.CharsetDecoder}類應在更多控件時使用
*需要解碼過程。
*
*@param字節
*要解碼爲字符的字節
*
*@param字符集
*{@link平原java.nio.charset文件.Charset Charset}用於
*解碼{@code bytes}
*
*@從1.6開始
*/
public String(byte bytes[], Charset charset) {
this(bytes, 0, bytes.length, charset);
}
/**
*通過解碼
*使用平臺默認字符集的字節。新的長度
*{@code String}是字符集的函數,因此可能不相等
*子陣列的長度。
*
*<p>給定字節無效時此構造函數的行爲
*在默認字符集中未指定。{@鏈接
* java.nio.charset文件.CharsetDecoder}類應在更多控件時使用
*需要解碼過程。
*
*@param字節
*要解碼爲字符的字節
*
*@param偏移量
*要解碼的第一個字節的索引
*
*@param長度
*要解碼的字節數
*
*@拋出IndexOutboundsException
*如果{@code offset}和{@code length}參數索引
*{@code bytes}數組界限之外的字符
*
*@從JDK1.1開始
*/
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
/**
*通過解碼指定的字節數組來構造新的{@code String}
*使用平臺的默認字符集。新{@代碼的長度
*String}是字符集的函數,因此可能不等於
*字節數組的長度。
*
*<p>給定字節無效時此構造函數的行爲
*在默認字符集中未指定。{@鏈接
* java.nio.charset文件.CharsetDecoder}類應在更多控件時使用
*需要解碼過程。
*
*@param字節
*要解碼爲字符的字節
*
*@從JDK1.1開始
*/
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
/**
*分配包含字符序列的新字符串
*當前包含在字符串緩衝區參數中。的內容
*複製字符串緩衝區;隨後修改字符串緩衝區
*不影響新創建的字符串。
*
*@param緩衝區
*{@code StringBuffer}
*/
public String(StringBuffer buffer) {
synchronized(buffer) {
this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
}
}
/**
*分配包含字符序列的新字符串
*當前包含在字符串生成器參數中。的內容
*字符串生成器被複制;字符串生成器的後續修改
*不影響新創建的字符串。
*
*<p>提供此構造函數是爲了方便遷移到{@code
*字符串生成器}。通過{@代碼從字符串生成器獲取字符串
*toString}方法可能運行得更快,通常是首選方法。
*
*@param生成器
*{@code StringBuilder}
*
*@從1.5開始
*/
public String(StringBuilder builder) {
this.value = Arrays.copyOf(builder.getValue(), builder.length());
}
/*
*爲速度共享值數組的包專用構造函數。
*此構造函數應始終使用share==true調用。
*需要一個單獨的構造函數,因爲我們已經有一個public
*String(char[])生成給定char[]副本的構造函數。
*/
String(char[] value, boolean share) {
//assert share:“不支持非共享”;
this.value = value;
}
/**
*返回此字符串的長度。
*長度等於字符.html#unicode“>Unicode碼
*字符串中的代碼單位</a>。
*
*@return這個字符序列的長度
*反對。
*/
public int length() {
return value.length;
}
/**
*返回{@code true}如果且僅當{@link#length()}是{@code 0}。
*
*@return{@code true}如果{@link#length()}是{@code 0},否則
*{@code假}
*
*@從1.6開始
*/
public boolean isEmpty() {
return value.length == 0;
}
/**
*返回位於
*指定的索引。索引的範圍從{@code 0}到
*{@code length()-1}。序列的第一個{@code char}值
*位於索引{@code 0},下一個位於索引{@code 1},
*等等,比如數組索引。
*
*<p>如果索引指定的{@code char}值是
*<a href=”字符.html#unicode“>代理項</a>,代理項
*返回值。
*
*@param index{@code char}值的索引。
*@返回此字符串的指定索引處的{@code char}值。
*第一個{@code char}值位於索引{@code 0}。
*如果{@code index}
*參數爲負數或不小於
*弦。
*/
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
/**
*返回指定位置的字符(Unicode代碼點)
*索引。索引引用{@code char}值
*(Unicode代碼單位),範圍從{@code 0}到
*{@link#length()}{@code-1}。
*
*<p>如果給定索引處指定的{@code char}值
*在高代理項範圍內,以下索引較少
*比這個{@code String}的長度,以及
*{@code char}以下索引處的值位於
*低代理項範圍,然後是補充代碼點
*將返回對應於此代理項對的。否則,
*返回給定索引處的{@code char}值。
*
*@param索引{@code char}值的索引
*@返回字符在
*{@code索引}
*如果{@code index}
*參數爲負數或不小於
*弦。
*@從1.5開始
*/
public int codePointAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, value.length);
}
/**
*返回指定的
*索引。索引引用{@code char}值
*(Unicode代碼單位),範圍從{@code 1}到{@link
*字符序列長度。
*
*<p>如果{@code(index-1)}處的{@code char}值
*在低代理項範圍內,{@code(index-2)}不
*負,並且{@code(索引)處的{@code char}值-
*2)}在高代理項範圍內,則
*代理項對的補充碼位值爲
*回來了。如果{@code char}值位於{@code index-
*1}是未配對的低代理項或高代理項
*返回代理項值。
*
*@param index應該返回的代碼點後面的索引
*@返回給定索引之前的Unicode碼位值。
*如果{@code index}
*參數小於1或大於長度
*這根繩子的。
*@從1.5開始
*/
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);
}
/**
*返回指定文本中Unicode代碼點的數目
*此{@code String}的範圍。文本範圍從
*指定{@code beginIndex},並擴展到
*{@code char}位於索引{@code endIndex-1}。因此
*文本範圍的長度(以{@code char}s爲單位)爲
*{@code-endIndex-beginIndex}。內的未配對代理
*每個文本區域計爲一個代碼點。
*
*@param beginIndex第一個{@code char}的索引
*文本範圍。
*@param endIndex最後一個{@code char}之後的索引
*文本範圍。
*@返回指定文本中Unicode代碼點的數目
*範圍
*@exception indexOutboundsException如果
*{@code beginIndex}爲負數,或{@code endIndex}
*大於此{@code String}的長度,或
*{@code beginIndex}大於{@code endIndex}。
*@從1.5開始
*/
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);
}
/**
*返回此{@code String}中的索引,即
*與給定{@code index}的偏移量
*{@code codePointOffset}代碼點。未配對代理
*在{@code index}和
*{@code codePointOffset}計算爲每個代碼點。
*
*@param index要偏移的索引
*@param codePointOffset代碼點中的偏移量
*@返回此{@code String}中的索引
*@exception indexOutboundsException如果{@code index}
*是負數或大於這個的長度
*{@code String},或者如果{@code codePointOffset}爲正
*以{@code index}開頭的子字符串
*而不是{@code codePointOffset}代碼點,
*或者{@code codePointOffset}爲負且子字符串
*前{@code index}的值小於絕對值
*{@code codePointOffset}個代碼點。
*@從1.5開始
*/
public int offsetByCodePoints(int index, int codePointOffset) {
if (index < 0 || index > value.length) {
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0, value.length,
index, codePointOffset);
}
/**
*將此字符串中的字符複製到從dstBegin開始的dst中。
*此方法不執行任何範圍檢查。
*/
void getChars(char dst[], int dstBegin) {
System.arraycopy(value, 0, dst, dstBegin, value.length);
}
/**
*將此字符串中的字符複製到目標字符中
*陣列。
*<p>
*要複製的第一個字符位於索引{@code srcBegin};
*要複製的最後一個字符位於索引{@code srcEnd-1}
*(因此,要複製的字符總數是
*{@code srcEnd srcegin})。字符被複制到
*從索引{@code dstBegin}開始的{@codedstt}子數組
*以索引結尾:
*<blockquote><pre>
*dstBegin+(srcEnd srcebegin)-1個
*</pre></blockquote>
*
*@param srcegin字符串中第一個字符的索引
*複製。
*@param srcent索引在字符串的最後一個字符之後
*複製。
*@param dst目標數組。
*@param dstbegen目標數組中的起始偏移量。
*@exception indexOutfoundsException如果出現以下任何情況
*是真的:
*<ul><li>{@code srcegin}爲負數。
*<li>{@code srcegin}大於{@code srcEnd}
*<li>{@code srcEnd}大於此
*字符串
*<li>{@code dstBegin}爲負數
*<li>{@code dstBegin+(srcEnd srcebegin)}大於
*{@代碼夏令時長度}</ul>
*/
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);
}
/**
*將此字符串中的字符複製到目標字節數組中。每個
*字節接收相應字符的8個低位。這個
*每個字符的八個高階位不會被複制
*以任何方式參與轉移。
*
*<p>要複製的第一個字符位於索引{@code srcBegin}
*要複製的最後一個字符位於索引{@code srcEnd-1}。總數
*要複製的字符數是{@code srcEnd srcegin}。這個
*轉換爲字節的字符被複制到{@代碼的子數組中
*dst}從索引{@code dstBegin}開始,到索引結束:
*
*<blockquote><pre>
*dstBegin+(srcEnd srcebegin)-1個
*</pre></blockquote>
*
*@已棄用此方法無法將字符正確轉換爲
*字節。從JDK 1.1開始,首選的方法是通過
*{@link#getBytes()}方法,它使用平臺的默認字符集。
*
*@param srcBegin參數
*要複製的字符串中第一個字符的索引
*
*@param srcEnd參數
*要複製的字符串中最後一個字符後的索引
*
*@param夏令時
*目標數組
*
*@param dstbegen參數
*目標數組中的起始偏移量
*
*@拋出IndexOutboundsException
*如果以下任何一項屬實:
*<ul>
*<li>{@code srcBegin}爲負數
*<li>{@code srcegin}大於{@code srcEnd}
*<li>{@code srcEnd}大於此字符串的長度
*<li>{@code dstBegin}爲負數
*<li>{@code dstBegin+(srcEnd srcebegin)}大於{@code
* 夏令時長度}
*</ul>
*/
@Deprecated
public void getBytes(int srcBegin, int srcEnd, byte 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);
}
Objects.requireNonNull(dst);
int j = dstBegin;
int n = srcEnd;
int i = srcBegin;
char[] val = value; /* avoid getfield opcode */
while (i < n) {
dst[j++] = (byte)val[i++];
}
}
/**
*使用
*字符集,將結果存儲到新的字節數組中。
*
*<p>此字符串無法編碼時此方法的行爲
*指定的字符集未指定。{@鏈接
* java.nio.charset文件.CharsetEncoder}類應在更多控件時使用
*需要通過編碼過程。
*
*@param字符集名稱
*支持的{@linkplain的名稱java.nio.charset文件.字符集
*字符集}
*
*@返回結果字節數組
*
*@throws不支持編碼異常
*如果不支持命名字符集
*
*@從JDK1.1開始
*/
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null) throw new NullPointerException();
return StringCoding.encode(charsetName, value, 0, value.length);
}
/**
*使用給定的
*{@link平原java.nio.charset文件.Charset Charset},將結果存儲到
*新字節數組。
*
*<p>此方法總是替換格式錯誤的輸入和不可映射的字符
*使用此字符集的默認替換字節數組的序列。這個
*{@鏈接java.nio.charset文件.CharsetEncoder}類應該在
*需要控制編碼過程。
*
*@param字符集
*{@link平原java.nio.charset文件.Charset}用於編碼
*{@code字符串}
*
*@返回結果字節數組
*
*@從1.6開始
*/
public byte[] getBytes(Charset charset) {
if (charset == null) throw new NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
}
/**
*使用
*平臺的默認字符集,將結果存儲到新的字節數組中。
*
*<p>此字符串無法編碼時此方法的行爲
*未指定默認字符集。{@鏈接
* java.nio.charset文件.CharsetEncoder}類應在更多控件時使用
*需要通過編碼過程。
*
*@返回結果字節數組
*
*@從JDK1.1開始
*/
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
/**
*將此字符串與指定對象進行比較。結果是{@code
*true}如果且僅當參數不是{@code null}並且是{@code
*String}對象,表示與此相同的字符序列
*反對。
*
*@param對象
*要與此{@code String}進行比較的對象
*
*如果給定對象表示{@code String},則返回{@code true}
*相當於此字符串,{@code false}否則
*
*@見35;比較器(字符串)
*@見35sIgnoreCase(字符串)
*
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;
}
/**
*將此字符串與指定的{@code StringBuffer}進行比較。結果
*{@code true}當且僅當此{@code String}表示相同時
*指定的字符序列{@code StringBuffer}。這種方法
*在{@code StringBuffer}上同步。
*
*@param某人
*將此{@code StringBuffer}與{@code String}進行比較的{@code StringBuffer}
*
*@return{@code true}如果這個{@code String}表示相同
*指定的字符序列{@code StringBuffer},
*{@code false}否則
*
*@從1.4開始
*/
public boolean contentEquals(StringBuffer sb) {
return contentEquals((CharSequence)sb);
}
private boolean nonSyncContentEquals(AbstractStringBuilder sb) {
char v1[] = value;
char v2[] = sb.getValue();
int n = v1.length;
if (n != sb.length()) {
return false;
}
for (int i = 0; i < n; i++) {
if (v1[i] != v2[i]) {
return false;
}
}
return true;
}
/**
*將此字符串與指定的{@code CharSequence}進行比較。這個
*結果是{@code true}如果且僅當此{@code String}表示
*字符值的序列與指定的序列相同。注意,如果
*{@code CharSequence}是一個{@code StringBuffer}然後是方法
*同步。
*
*@param cs參數
*將此{@code String}與
*
*@return{@code true}如果這個{@code String}表示相同
*指定的字符值序列{@code
*假}否則
*
*@從1.5開始
*/
public boolean contentEquals(CharSequence cs) {
//參數是StringBuffer,StringBuilder
if (cs instanceof AbstractStringBuilder) {
if (cs instanceof StringBuffer) {
synchronized(cs) {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
} else {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
//參數是字符串
if (cs instanceof String) {
return equals(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;
}
/**
*將此{@code String}與另一個{@code String}進行比較,忽略大小寫
*注意事項。兩個字符串被視爲相等忽略大小寫
*在兩個字符串中具有相同的長度和相應的字符
*等於忽略大小寫。
*
*<p>兩個字符{@code c1}和{@code c2}被認爲是相同的
*如果以下至少一項爲真,則忽略大小寫:
*<ul>
*<li>兩個字符相同(與
*{@code=}運算符)
*<li>應用方法{@link
* java.lang.Character#toUpperCase(char)}到每個字符
*產生相同的結果
*<li>應用方法{@link
* java.lang.Character#toLowerCase(char)}到每個字符
*產生相同的結果
*</ul>
*
*@param其他字符串
*將此{@code String}與{@code String}進行比較的{@code String}
*
*@return{@code true}如果參數不是{@code null},並且
*表示等效的{@code String}忽略大小寫;{@code
*假}否則
*
*@see#equals(對象)
*/
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
/**
*從詞彙上比較兩個字符串。
*比較基於中每個字符的Unicode值
*弦。用這個表示的字符序列
*{@code String}對象與
*由參數字符串表示的字符序列。結果是
*如果此{@code String}對象爲負整數
*在詞彙上位於參數字符串之前。結果是
*如果此{@code String}對象按字典順序排列,則爲正整數
*在參數字符串後面。如果字符串
*等於;{@code compareTo}返回{@code 0}正好在
*{@link\equals(Object)}方法將返回{@code true}。
*<p>
*這就是詞典排序的定義。如果兩個字符串
*不同,那麼它們在某個索引處有不同的字符
*這是兩個字符串的有效索引,或者它們的長度不同,
*或者兩者都有。如果它們在一個或多個索引處有不同的字符
*位置,設<i>k</i>爲最小的索引;然後字符串
*其位置<i>k</i>處的字符具有較小的值,如
*通過使用<;運算符來確定,在
*其他字符串。在這種情況下,{@code compareTo}返回
*中{@code k}位置兩個字符值的差異
*兩個字符串——即,值:
*<blockquote><pre>
* 這個角色(千)-另一個字符串.charAt(千)
*</pre></blockquote>
*如果沒有不同的索引位置,則
*字符串在詞彙上先於較長的字符串。在這種情況下,
*{@code compareTo}返回
*字符串——即,值:
*<blockquote><pre>
* 這個長度()-另一個字符串。長度()
*</pre></blockquote>
*
*@param anotherString要比較的{@code String}。
*@如果參數字符串等於
*此字符串;如果此字符串小於{@code 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;
}
/**
*排序{@code String}對象的比較器
*{@code compareToIgnoreCase}。這個比較器是可序列化的。
*<p>
*注意,這個比較器不考慮語言環境,
*並且會導致某些地區的訂單不滿意。
*那個java.text語言包提供<em>collator</em>以允許
*區分區域設置的排序。
*
*@看java.text.Collator比較(字符串,字符串)
*@從1.2開始
*/
public static final Comparator<String> CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
implements Comparator<String>, java.io.Serializable {
//使用JDK 1.2.2中的serialVersionUID實現互操作性
private static final long serialVersionUID = 8575799808933029326L;
public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
//由於數值提升,沒有溢出
return c1 - c2;
}
}
}
}
return n1 - n2;
}
/** Replaces the de-serialized object. */
private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
}
/**
*按字典順序比較兩個字符串,忽略大小寫
*差異。此方法返回一個整數,其符號爲
*用字符串的規範化版本調用{@code compareTo}
*通過調用
*{@代碼字符.toLowerCase(字符.toUpperCase(字符)}打開
*每個角色。
*<p>
*注意,此方法不考慮區域設置,
*並且會導致某些地區的訂單不滿意。
*那個java.text語言包提供<em>collator</em>以允許
*區分區域設置的排序。
*
*@param str要比較的{@code String}。
*@返回一個負整數、零或正整數作爲
*指定的字符串大於、等於或小於
*而不是這個字符串,忽略大小寫。
*@看java.text.Collator比較(字符串,字符串)
*@從1.2開始
*/
public int compareToIgnoreCase(String str) {
return CASE_INSENSITIVE_ORDER.compare(this, str);
}
/**
*測試兩個字符串區域是否相等。
*<p>
*將此{@code String}對象的子字符串與子字符串進行比較
*爭論的另一方。如果這些子字符串
*表示相同的字符序列。這個的子串
*{@code String}要比較的對象從索引{@code toffset}開始
*長度爲{@code len}。其他要比較的子串
*從索引{@code ooffset}開始,長度爲{@code len}。這個
*結果是{@code false}如果且僅當以下條件之一
*是真的:
*<ul><li>{@code toffset}爲負數。
*<li>{@code ooffset}爲負數。
*<li>{@code toffset+len}大於
*{@code String}對象。
*<li>{@code ooffset+len}大於另一個的長度
*爭論。
*<li>有一些非負整數<i>k</i>小於{@code len}
*以便:
*{@代碼這個角色(toffset+}<i>k</i>{@code)!= 其他字符(ooffset+}
*<i>k</i>{@code}
*</ul>
*
*@param toffset此字符串中子區域的起始偏移量。
*@param其他字符串參數。
*@param ooffset字符串中子區域的起始偏移量
*爭論。
*@param len要比較的字符數。
*@return{@code true}如果此字符串的指定子區域
*與字符串參數的指定子區域完全匹配;
*{@code false}否則。
*/
public boolean regionMatches(int toffset, String other, int ooffset,
int len) {
char ta[] = value;
int to = toffset;
char pa[] = other.value;
int po = ooffset;
//注意:toffset、ooffset或len可能接近-1>>>1。
if ((ooffset < 0) || (toffset < 0)
|| (toffset > (long)value.length - len)
|| (ooffset > (long)other.value.length - len)) {
return false;
}
while (len-- > 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
/**
*測試兩個字符串區域是否相等。
*<p>
*將此{@code String}對象的子字符串與子字符串進行比較
*參數的{@code other}。結果是{@code true}如果
*子字符串表示相同的字符序列,忽略
*case if且僅當{@code ignoreCase}爲真。子串
*要比較的{@code String}對象從索引開始
*{@code toffset}的長度爲{@code len}。子串
*待比較的{@code other}從索引{@code ooffset}開始
*有長度{@code len}。結果是{@code false}如果且僅當
*以下至少一項是正確的:
*<ul><li>{@code toffset}爲負數。
*<li>{@code ooffset}爲負數。
*<li>{@code toffset+len}大於
*{@code String}對象。
*<li>{@code ooffset+len}大於另一個的長度
*爭論。
*<li>{@code ignoreCase}是{@code false},並且有一些非負的
*小於{@code len}的整數:
*<blockquote><pre>
* 這個角色(toffset+k)!= 其他字符(ooffset+k)
*</pre></blockquote>
*<li>{@code ignoreCase}是{@code true},並且有一些非負的
*小於{@code len}的整數:
*<blockquote><pre>
* 字符.toLowerCase(這個角色(toffset+k)!=
Character.toLowerCase(other.charAt(ooffset+k))
*</pre></blockquote>
*以及:
*<blockquote><pre>
* 字符.toUpperCase(這個角色(toffset+k)!=
* 字符.toUpperCase(其他字符(ooffset+k)
*</pre></blockquote>
*</ul>
*
*@param ignore case如果{@code true},比較時忽略大小寫
*角色。
*@param toffset此子區域的起始偏移
*弦。
*@param其他字符串參數。
*@param ooffset字符串中子區域的起始偏移量
*爭論。
*@param len要比較的字符數。
*@return{@code true}如果此字符串的指定子區域
*匹配字符串參數的指定子區域;
*{@code false}否則。匹配是否準確
*或者不區分大小寫取決於{@code ignoreCase}
*爭論。
*/
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len) {
char ta[] = value;
int to = toffset;
char pa[] = other.value;
int po = ooffset;
//注意:toffset、ooffset或len可能接近-1>>>1。
if ((ooffset < 0) || (toffset < 0)
|| (toffset > (long)value.length - len)
|| (ooffset > (long)other.value.length - len)) {
return false;
}
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
if (c1 == c2) {
continue;
}
if (ignoreCase) {
//如果字符不匹配但大小寫可以忽略,
//嘗試將兩個字符都轉換爲大寫。
//如果結果匹配,則比較掃描應
//繼續。
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
//不幸的是,轉換成大寫字母不能正常工作
//對於格魯吉亞字母表,有着奇怪的規則
//轉換。所以我們需要在
//正在退出。
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
/**
*測試此字符串的子字符串是否以
*指定的索引以指定的前綴開頭。
*
*@param前綴前綴。
*@param toffset從何處開始查找此字符串。
*@return{@code true}如果
*參數是此對象開始的子字符串的前綴
*在索引{@code toffset};否則{@code false}。
*如果{@code toffset}是
*負值或大於此長度
*{@code String}對象;否則結果相同
*作爲表達式的結果
*<pre>
* this.子字符串(toffset).startsWith(前綴)
*</pre>
*/
public boolean startsWith(String prefix, int toffset) {
char ta[] = value;
int to = toffset;
char pa[] = prefix.value;
int po = 0;
int pc = prefix.value.length;
//注意:toffset可能接近-1>>>1。
if ((toffset < 0) || (toffset > value.length - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
/**
*測試此字符串是否以指定的前綴開頭。
*
*@param前綴前綴。
*@return{@code true}如果
*參數是字符序列的前綴,表示爲
*這個字符串;{@code false}否則。
*還要注意,如果
*參數爲空字符串或等於
*{@code String}對象,由
*{@link\equals(Object)}方法。
*@從1開始。0個
*/
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
/**
*測試此字符串是否以指定的後綴結尾。
*
*@param後綴後綴。
*@return{@code true}如果
*參數是字符序列的後綴,表示爲
*這個對象;{@code false}否則。請注意
*如果參數是
*空字符串或等於此{@code string}對象
*由{@link#equals(Object)}方法確定。
*/
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
/**
*返回此字符串的哈希代碼。的哈希代碼
*{@code String}對象計算爲
*<blockquote><pre>
*s[0]*31^(n-1)+s[1]*31^(n-2)+。。。+s[n-1]
*</pre></blockquote>
*使用{@code int}算術,其中{@code s[i]}是
*<i>i</i>字符串的第個字符{@code n}是
*字符串和{@code^}表示指數運算。
*(空字符串的哈希值爲零。)
*
*@返回此對象的哈希代碼值。
*/
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;
}
/**
*返回此字符串中出現的第一個
*指定的字符。如果一個有價值的字符
*{@code ch}出現在由
*這個{@code String}對象,然後是索引(Unicode格式
*返回第一個這樣的事件的代碼單位)。爲了
*{@code ch}的值,範圍從0到0xFFFF
*(包括在內),這是最小值<i>k</i>,以便:
*<blockquote><pre>
* 這個角色(<i>k</i>)==ch
*</pre></blockquote>
*是真的。對於{@code ch}的其他值,它是
*最小值<i>k</i>如下:
*<blockquote><pre>
* this.codePointAt這個(<i>k</i>)==ch
*</pre></blockquote>
*是真的。在這兩種情況下,如果在
*字符串,然後返回{@code-1}。
*
*@param ch a字符(Unicode碼位)。
*@返回
*此對象表示的字符序列,或
*{@code-1}如果字符沒有出現。
*/
public int indexOf(int ch) {
return indexOf(ch, 0);
}
/**
*返回此字符串中第一個出現的
*指定字符,從指定索引開始搜索。
*<p>
*如果值爲{@code ch}的字符出現在
*由這個{@code String}表示的字符序列
*對象的索引不小於{@code fromIndex},然後
*返回第一個此類事件的索引。價值觀
*從0到0xFFFF(含)範圍內的{@code ch},
*這是最小值<i>k</i>,以便:
*<blockquote><pre>
* (這個角色(<i>k</i>)==ch){@code&&}(<i>k</i>>;=來自索引)
*</pre></blockquote>
*是真的。對於{@code ch}的其他值,它是
*最小值<i>k</i>如下:
*<blockquote><pre>
* (this.codePointAt這個(<i>k</i>)==ch){@code&&}(<i>k</i>>;=來自索引)
*</pre></blockquote>
*是真的。在這兩種情況下,如果在
*在{@code frommindex}位置或之後的字符串,然後
*{@code-1}返回。
*
*<p>
*對{@code fromIndex}的值沒有限制。如果它
*是負的,它的效果和它爲零一樣:整個
*可以搜索字符串。如果它大於這個長度
*字符串,其效果與
*返回此字符串{@code-1}。
*
*<p>所有索引都以{@code char}值指定
*(Unicode代碼單位)。
*
*@param ch a字符(Unicode碼位)。
*@param from索引開始搜索的索引。
*@返回
*此對象表示的字符序列
*大於或等於{@code fromIndex},或{@code-1}
*如果字符沒有出現。
*/
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
//注意:fromIndex可能接近-1>>>1。
return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
//在這裏處理大多數情況(ch是BMP代碼點或
//負值(無效代碼點)
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);
}
}
/**
*處理帶有補充字符的indexOf調用(罕見)。
*/
private int indexOfSupplementary(int ch, int fromIndex) {
if (Character.isValidCodePoint(ch)) {
final char[] value = this.value;
final char hi = Character.highSurrogate(ch);
final char lo = Character.lowSurrogate(ch);
final int max = value.length - 1;
for (int i = fromIndex; i < max; i++) {
if (value[i] == hi && value[i + 1] == lo) {
return i;
}
}
}
return -1;
}
/**
*返回此字符串中最後出現的
*指定的字符。中{@code ch}的值
*範圍從0到0xFFFF(包含),索引(以Unicode代碼表示
*單位)返回的是最大值<i>k</i>,以便:
*<blockquote><pre>
* 這個角色(<i>k</i>)==ch
*</pre></blockquote>
*是真的。對於{@code ch}的其他值,它是
*最大值<i>k</i>如下:
*<blockquote><pre>
* this.codePointAt這個(<i>k</i>)==ch
*</pre></blockquote>
*是真的。在這兩種情況下,如果在
*字符串,然後返回{@code-1}。這個
*{@code String}從最後一個開始向後搜索
*性格。
*
*@param ch a字符(Unicode碼位)。
*@返回
*此對象表示的字符序列,或
*{@code-1}如果字符沒有出現。
*/
public int lastIndexOf(int ch) {
return lastIndexOf(ch, value.length - 1);
}
/**
*返回此字符串中最後出現的
*指定的字符,從
*指定的索引。對於範圍內{@code ch}的值
*從0到0xFFFF(包括),返回的索引是最大的
*值<i>k</i>以便:
*<blockquote><pre>
* (這個角色(<i>k</i>)==ch){@code&&}(<i>k</i><;=來自索引)
*</pre></blockquote>
*是真的。對於{@code ch}的其他值,它是
*最大值<i>k</i>如下:
*<blockquote><pre>
* (this.codePointAt這個(<i>k</i>)==ch){@code&&}(<i>k</i><;=來自索引)
*</pre></blockquote>
*是真的。在這兩種情況下,如果在
*在{@code frommindex}位置或之前的字符串,然後
*{@code-1}返回。
*
*<p>所有索引都以{@code char}值指定
*(Unicode代碼單位)。
*
*@param ch a字符(Unicode碼位)。
*@param from索引開始搜索的索引。沒有
*對{@code fromIndex}的值的限制。如果是的話
*大於或等於此字符串的長度
*其效果與
*此字符串的長度:可以搜索整個字符串。
*如果是負數,則其效果與-1相同:
*返回-1。
*@返回
*此對象表示的字符序列
*大於或等於{@code fromIndex},或{@code-1}
*如果字符不在該點之前出現。
*/
public int lastIndexOf(int ch, int fromIndex) {
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
//在這裏處理大多數情況(ch是BMP代碼點或
//負值(無效代碼點)
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);
}
}
/**
*處理帶有補充字符的lastIndexOf調用(罕見)。
*/
private int lastIndexOfSupplementary(int ch, int fromIndex) {
if (Character.isValidCodePoint(ch)) {
final char[] value = this.value;
char hi = Character.highSurrogate(ch);
char lo = Character.lowSurrogate(ch);
int i = Math.min(fromIndex, value.length - 2);
for (; i >= 0; i--) {
if (value[i] == hi && value[i + 1] == lo) {
return i;
}
}
}
return -1;
}
/**
*返回此字符串中第一個出現的
*指定的子字符串。
*
*<p>返回的索引是最小值<i>k</i>,其中:
*<blockquote><pre>
* 這個。開始(str,<i>k</i>)
*</pre></blockquote>
*如果不存在這樣的值<i>k</i>,則返回{@code-1}。
*
*@param str要搜索的子字符串。
*@返回指定子字符串第一次出現的索引,
*或者{@code-1}如果沒有這種情況。
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
*返回此字符串中第一個出現的
*指定的子字符串,從指定的索引開始。
*
*<p>返回的索引是最小值<i>k</i>,其中:
*<blockquote><pre>
*<i>k</i>>;=來自索引{@code&&}這個。開始(str,<i>k</i>)
*</pre></blockquote>
*如果不存在這樣的值<i>k</i>,則返回{@code-1}。
*
*@param str要搜索的子字符串。
*@param fromIndex開始搜索的索引。
*@返回指定子字符串第一次出現的索引,
*從指定索引開始,
*或者{@code-1}如果沒有這種情況。
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
*由String和AbstractStringBuilder共享以執行搜索的代碼。這個
*源是正在搜索的字符數組,目標是
*是正在搜索的字符串。
*
*@param source正在搜索的字符。
*@param源字符串的source offset偏移量。
*@param源字符串的source count計數。
*@param以正在搜索的字符爲目標。
*@param from索引開始搜索的索引。
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex) {
return indexOf(source, sourceOffset, sourceCount,
target.value, 0, target.value.length,
fromIndex);
}
/**
*由String和StringBuffer共享以執行搜索的代碼。這個
*源是正在搜索的字符數組,目標是
*是正在搜索的字符串。
*
*@param source正在搜索的字符。
*@param源字符串的source offset偏移量。
*@param源字符串的source count計數。
*@param以正在搜索的字符爲目標。
*@param target offset目標字符串的偏移量。
*@param target count目標字符串的計數。
*@param from索引開始搜索的索引。
*/
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);
if (j == end) {
/* Found whole string. */
return i - sourceOffset;
}
}
}
return -1;
}
/**
*返回此字符串中最後出現的
*指定的子字符串。空字符串“”的最後一次出現
*被認爲出現在索引值{@code這個長度()}.
*
*<p>返回的索引是最大值<i>k</i>,其中:
*<blockquote><pre>
* 這個。開始(str,<i>k</i>)
*</pre></blockquote>
*如果不存在這樣的值<i>k</i>,則返回{@code-1}。
*
*@param str要搜索的子字符串。
*@返回指定子字符串最後一次出現的索引,
*或者{@code-1}如果沒有這種情況。
*/
public int lastIndexOf(String str) {
return lastIndexOf(str, value.length);
}
/**
*返回此字符串中最後出現的
*指定的子字符串,從指定索引開始向後搜索。
*
*<p>返回的索引是最大值<i>k</i>,其中:
*<blockquote><pre>
*<i>k</i>{@code<=}來自索引{@code&&}這個。開始(str,<i>k</i>)
*</pre></blockquote>
*如果不存在這樣的值<i>k</i>,則返回{@code-1}。
*
*@param str要搜索的子字符串。
*@param from索引開始搜索的索引。
*@返回指定子字符串最後一次出現的索引,
*從指定索引向後搜索,
*或者{@code-1}如果沒有這種情況。
*/
public int lastIndexOf(String str, int fromIndex) {
return lastIndexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
*由String和AbstractStringBuilder共享以執行搜索的代碼。這個
*源是正在搜索的字符數組,目標是
*是正在搜索的字符串。
*
*@param source正在搜索的字符。
*@param源字符串的source offset偏移量。
*@param源字符串的source count計數。
*@param以正在搜索的字符爲目標。
*@param from索引開始搜索的索引。
*/
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex) {
return lastIndexOf(source, sourceOffset, sourceCount,
target.value, 0, target.value.length,
fromIndex);
}
/**
*由String和StringBuffer共享以執行搜索的代碼。這個
*源是正在搜索的字符數組,目標是
*是正在搜索的字符串。
*
*@param source正在搜索的字符。
*@param源字符串的source offset偏移量。
*@param源字符串的source count計數。
*@param以正在搜索的字符爲目標。
*@param target offset目標字符串的偏移量。
*@param target count目標字符串的計數。
*@param from索引開始搜索的索引。
*/
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
/*
*檢查參數;儘可能立即返回。爲了
*一致性,不要檢查空str。
*/
int rightIndex = sourceCount - targetCount;
if (fromIndex < 0) {
return -1;
}
if (fromIndex > rightIndex) {
fromIndex = rightIndex;
}
/* Empty string always matches. */
if (targetCount == 0) {
return fromIndex;
}
int strLastIndex = targetOffset + targetCount - 1;
char strLastChar = target[strLastIndex];
int min = sourceOffset + targetCount - 1;
int i = min + fromIndex;
startSearchForLastChar:
while (true) {
while (i >= min && source[i] != strLastChar) {
i--;
}
if (i < min) {
return -1;
}
int j = i - 1;
int start = j - (targetCount - 1);
int k = strLastIndex - 1;
while (j > start) {
if (source[j--] != target[k--]) {
i--;
continue startSearchForLastChar;
}
}
return start - sourceOffset + 1;
}
}
/**
*返回此字符串的子字符串。這個
*子字符串以指定索引處的字符開頭,並且
*擴展到此字符串的結尾。<p>
*示例:
*<blockquote><pre>
*“不快樂”。子字符串(2)返回“快樂”
*“Harbison”.substring(3)返回“bison”
*“空”。子字符串(9)返回“”(空字符串)
*</pre></blockquote>
*
*@param beginIndex開始索引,包含在內。
*@返回指定的子字符串。
*@exception indexOutboundsException如果
*{@code beginIndex}爲負數或大於
*這個{@code String}對象的長度。
*/
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);
}
/**
*返回此字符串的子字符串。這個
*子字符串從指定的{@code beginIndex}開始
*擴展到索引{@code endIndex-1}處的字符。
*因此子字符串的長度是{@code endIndex beginIndex}。
*<p>
*示例:
*<blockquote><pre>
*“漢堡包”。子字符串(4,8)返回“敦促”
*“smiles”。子字符串(1,5)返回“mile”
*</pre></blockquote>
*
*@param beginIndex開始索引,包含在內。
*@param endIndex結束索引,獨佔。
*@返回指定的子字符串。
*@exception indexOutboundsException如果
*{@code beginIndex}爲負數,或
*{@code endIndex}大於
*這個{@code String}對象,或者
*{@code beginIndex}大於
*{@code endIndex}。
*/
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);
}
/**
*返回作爲此序列的子序列的字符序列。
*
*<p>調用此窗體方法
*
*<blockquote><pre>
* str.子序列(開始,結束)</pre></blockquote>
*
*行爲方式與調用完全相同
*
*<blockquote><previous>
*str.子串(開始,結束)</pre></blockquote>
*
*@阿皮諾特
*定義此方法是爲了{@code String}類可以實現
*{@link CharSequence}接口。
*
*@param begin index開始索引,包含在內。
*@param end index結束索引,獨佔。
*@返回指定的子序列。
*
*@拋出IndexOutboundsException
*如果{@code beginIndex}或{@code endIndex}爲負,
*如果{@code endIndex}大於{@code length()},
*或者{@code beginIndex}大於{@code endIndex}
*
*@從1.4開始
*@規範JSR-51
*/
public CharSequence subSequence(int beginIndex, int endIndex) {
return this.substring(beginIndex, endIndex);
}
/**
*將指定的字符串連接到此字符串的結尾。
*<p>
*如果參數字符串的長度是{@code 0},則
*{@code String}返回對象。否則,a
*{@code String}返回表示字符的對象
*序列是字符序列的連接
*由這個{@code String}對象和字符表示
*由參數字符串表示的序列。<p>
*示例:
*<blockquote><pre>
*“cares”.concat(“s”)返回“caress”
*“to”.concat(“get”).concat(“her”)返回“together”
*</pre></blockquote>
*
*@param str連接到結尾的{@code String}
*這個{@code 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);
}
/**
*返回替換所有出現的
*{@code oldChar}在這個字符串中使用{@code newChar}。
*<p>
*如果字符{@code oldChar}不出現在
*這個{@code String}對象表示的字符序列,
*然後返回對這個{@code String}對象的引用。
*否則,返回{@code String}對象
*表示與字符序列相同的字符序列
*由這個{@code String}對象表示,除了
*{@code oldChar}的出現被替換爲
*屬於{@code newChar}。
*<p>
*示例:
*<blockquote><pre>
*“梅斯基特在你的地窖裏”。替換('e','o')
*返回“衣領裏有蚊子”
*“男爵之戰”。replace('r','y')
*返回“刺刀的方式”
*“與紫色海豚搏鬥”。替換('p','t')
*返回“與海龜一起主演”
*“JonL.replace('q','x')返回“JonL”(無更改)
*</pre></blockquote>
*
*@param oldChar舊字符。
*@param newChar新字符。
*@通過替換
*{@code oldChar}與{@code 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;
}
/**
*指示此字符串是否與給定的
*ref=“../util/regex”/Pattern.html總和“>正則表達式</a>。
*
*<p>調用此窗體方法
*<i>str</i>{@code.matches(}<i>regex</i>{@code)}精確地生成
*與表達式相同的結果
*
*<blockquote>
*{@鏈接java.util.regex.Pattern}.{@linkjava.util.regex.Pattern#匹配(字符串、CharSequence)
*匹配(<i>regex</i>,<i>str</i>)}
*</blockquote>
*
*@param正則表達式
*要與此字符串匹配的正則表達式
*
*@return{@code true}如果且僅當此字符串與
*給定正則表達式
*
*@throws模式語法異常
*如果正則表達式的語法無效
*
*@看java.util.regex.圖案
*
*@從1.4開始
*@規範JSR-51
*/
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
/**
*僅當此字符串包含指定的
*字符值的序列。
*
*@param s要搜索的序列
*@return true如果此字符串包含{@code s},則返回false,否則返回false
*@從1.5開始
*/
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
/**
*替換此字符串的第一個子字符串,該子字符串與給定的
*ref=“../util/regex”/Pattern.html總和“>正則表達式
*給予替換。
*
*<p>調用此窗體方法
*<i>str</i>{@code.replaceFirst(}<i>regex</i>{@code,}<i>repl</i>{@code)}
*產生與表達式完全相同的結果
*
*<blockquote>
*<代碼>
*{@鏈接java.util.regex.Pattern}.{@link
* java.util.regex.Pattern#編譯編譯}(<i>regex</i>)。{@link
* java.util.regex.模式匹配器(java.lang.CharSequence語言)匹配器}(<i>str</i>)。{@link
* java.util.regex.Matcher 35;替換第一個替換第一個}(<i>repl</i>)
*</code>
*</blockquote>
*
*<p>
*請注意,在
*替換字符串可能導致結果不同於
*作爲文本替換字符串處理;請參見
*{@鏈接java.util.regex.Matcher#替換第一個}。
*使用{@linkjava.util.regex.Matcher#quoteReplacement}以抑制特殊
*如果需要,這些字符的含義。
*
*@param正則表達式
*要與此字符串匹配的正則表達式
*@param替換
*要替換第一個匹配項的字符串
*
*@返回結果{@code String}
*
*@throws模式語法異常
*如果正則表達式的語法無效
*
*@看java.util.regex.圖案
*
*@從1.4開始
*@規範JSR-51
*/
public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}
/**
*替換此字符串中與給定的
*ref=“../util/regex”/Pattern.html總和“>正則表達式
*給予替換。
*
*<p>調用此窗體方法
*<i>str</i>{@code.replaceAll(}<i>regex</i>{@code,}<i>repl</i>{@code)}
*產生與表達式完全相同的結果
*
*<blockquote>
*<代碼>
*{@鏈接java.util.regex.Pattern}.{@link
* java.util.regex.Pattern#編譯編譯}(<i>regex</i>)。{@link
* java.util.regex.模式匹配器(java.lang.CharSequence語言)匹配器}(<i>str</i>)。{@link
* java.util.regex.Matcher 35; replaceAll replaceAll}(<i>repl</i>)
*</code>
*</blockquote>
*
*<p>
*請注意,在
*替換字符串可能導致結果不同於
*作爲文本替換字符串處理;請參見
*{@鏈接java.util.regex.Matcher#替換所有Matcher.replaceAll匹配器}.
*使用{@linkjava.util.regex.Matcher#quoteReplacement}以抑制特殊
*如果需要,這些字符的含義。
*
*@param正則表達式
*要與此字符串匹配的正則表達式
*@param替換
*每個匹配項要替換的字符串
*
*@返回結果{@code String}
*
*@throws模式語法異常
*如果正則表達式的語法無效
*
*@看java.util.regex.圖案
*
*@從1.4開始
*@規範JSR-51
*/
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
/**
*替換此字符串中與文本目標匹配的每個子字符串
*具有指定文字替換序列的序列。這個
*替換從字符串的開頭到結尾進行,用於
*例如,將字符串“aaa”中的“aa”替換爲“b”將導致
*“ba”而不是“ab”。
*
*@param target要替換的字符值序列
*@param replacement字符值的替換序列
*@返回結果字符串
*@從1.5開始
*/
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
/**
*在給定的
*<a href=“../util/regex”/Pattern.html總和“>正則表達式</a>。
*
*<p>此方法返回的數組包含
*由另一個子字符串終止的字符串,該子字符串與給定的
*表達式或在字符串結尾處終止。中的子字符串
*數組按它們在此字符串中出現的順序排列。如果
*表達式與輸入的任何部分都不匹配,則與結果數組不匹配
*只有一個元素,即這個字符串。
*
*<p>當開始處有正寬度匹配時
*字符串,則在開頭包含一個空前導子字符串
*結果數組的。但是,開頭的零寬度匹配
*從不產生這樣空的前導子字符串。
*
*<p>參數{@code limit}控制
*應用模式,因此會影響結果的長度
*陣列。如果極限<i>n</i>大於零,則模式
*最多應用<i>n</i>-1次,數組的
*長度將不大於<i>n</i>,並且數組的最後一個條目
*將包含最後一個匹配分隔符以外的所有輸入。如果<i>n</i>
*如果爲非正,則該模式將被應用
*可能,數組可以有任意長度。如果<i>n</i>爲零,則
*該模式將被儘可能多次應用,數組可以
*任何長度,都將丟棄尾隨的空字符串。
*
*<p>字符串{@code“布:還有:foo“}例如,生成
*以下是這些參數的結果:
*
*<blockquote><table cellpadding=1 cellspacing=0 summary=“顯示regex、limit和result的拆分示例”>
*<tr>
*<th>正則表達式</th>
*<th>限制</th>
*<th>結果</th>
*</tr>
*<tr><td align=center>:</td>
*<td align=中心>2</td>
*<td>{@code{“boo”和:foo“}}</td></tr>
*<tr><td align=center>:</td>
*<td align=中心>5</td>
*<td>{@code{“boo”,“and”,“foo”}</td></tr>
*<tr><td align=center>:</td>
*<td align=中心>-2</td>
*<td>{@code{“boo”,“and”,“foo”}</td></tr>
*<tr><td align=center>o</td>
*<td align=中心>5</td>
*<td>{@code{“b”,“”,“:和:f“,”,“,”}</td></tr>
*<tr><td align=center>o</td>
*<td align=中心>-2</td>
*<td>{@code{“b”,“”,“:和:f“,”,“,”}</td></tr>
*<tr><td align=center>o</td>
*<td align=中心>0</td>
*<td>{@code{“b”,“”,“:和:f“}}</td></tr>
*</table></blockquote>
*
*<p>調用此窗體方法
*<i>str.</i>{@代碼拆分(}<i>regex</i>{@code,}<i>n</i>{@code)}
*產生與表達式相同的結果
*
*<blockquote>
*<代碼>
*{@鏈接java.util.regex.Pattern}.{@link
* java.util.regex.Pattern#編譯編譯}(<i>regex</i>)。{@link
* java.util.regex.圖案#分割(java.lang.CharSequence語言,int)拆分}(<i>str</i>,<i>n</i>)
*</code>
*</blockquote>
*
*
*@param正則表達式
*定界正則表達式
*
*@param限制
*結果閾值,如上所述
*
*@返回通過拆分此字符串計算的字符串數組
*關於給定正則表達式的匹配
*
*@throws模式語法異常
*如果正則表達式的語法無效
*
*@看java.util.regex.圖案
*
*@從1.4開始
*@規範JSR-51
*/
public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else { // last one
//斷言(列表大小(==限值-1);
list.add(substring(off, value.length));
off = value.length;
break;
}
}
//如果找不到匹配項,請返回此
if (off == 0)
return new String[]{this};
//添加剩餘段
if (!limited || list.size() < limit)
list.add(substring(off, value.length));
//構造結果
int resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
/**
*將此字符串拆分爲
*ref=“../util/regex”/Pattern.html總和“>正則表達式</a>。
*
*<p>此方法的工作方式好像是通過調用兩個參數{@link
*具有給定表達式和限制的split(String,int)split}方法
*零的論點。因此,後面的空字符串不包括在
*結果數組。
*
*<p>字符串{@code“布:還有:foo“}例如,產生以下結果
*這些表達式的結果:
*
*<blockquote><table cellpadding=1 cellspacing=0 summary=“顯示正則表達式和結果的拆分示例”>
*<tr>
*<th>正則表達式</th>
*<th>結果</th>
*</tr>
*<tr><td align=center>:</td>
*<td>{@code{“boo”,“and”,“foo”}</td></tr>
*<tr><td align=center>o</td>
*<td>{@code{“b”,“”,“:和:f“}}</td></tr>
*</table></blockquote>
*
*
*@param正則表達式
*定界正則表達式
*
*@返回通過拆分此字符串計算的字符串數組
*關於給定正則表達式的匹配
*
*@throws模式語法異常
*如果正則表達式的語法無效
*
*@看java.util.regex.圖案
*
*@從1.4開始
*@規範JSR-51
*/
public String[] split(String regex) {
return split(regex, 0);
}
/**
*返回由
*{@code CharSequence元素}與
*指定的{@code分隔符}。
*
*<blockquote>例如,
*<pre>{@代碼
*字符串消息=字符串.join(“-”、“Java”、“is”、“cool”);
*//返回的消息是:“Java很酷”
*}</pre></blockquote>
*
*注意,如果元素爲空,則添加{@code“null”}。
*
*@param delimiter分隔每個元素的分隔符
*@param元素要連接在一起的元素。
*
*@返回一個由{@code元素組成的新{@code字符串}
*由{@code分隔符}分隔
*
*如果{@code delimiter}或{@code elements},則@throws爲NullPointerException
*是{@code null}
*
*@看java.util.StringJoiner
*@從1.8開始
*/
public static String join(CharSequence delimiter, CharSequence... elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
//不可能值的元素數數組.stream頭頂上。
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
/**
*返回由
*{@code CharSequence元素}與
*指定了{@code delimiter}。
*
*<blockquote>例如,
*<pre>{@代碼
*List<String>strings=new LinkedList<>();
* 字符串.add(“Java”);字符串.add(“is”);
* 字符串.add(“酷”);
*字符串消息=字符串.join(“”字符串);
*//返回的消息是:“Java很酷”
*
*Set<String>strings=new LinkedHashSet<>();
* 字符串.add(“Java”);字符串.add(“is”);
* 字符串.add(“非常”);字符串.add(“酷”);
*字符串消息=字符串.join(“-”,字符串);
*//返回的消息是:“Java非常酷”
*}</pre></blockquote>
*
*注意,如果單個元素是{@code null},則添加{@code“null”}。
*
*@param delimiter用於分隔每個
*結果{@code String}中的{@code元素}
*@param elements一個{@code Iterable},它將有自己的{@code elements}
*結合在一起。
*
*@返回由{@code元素}組成的新{@code字符串}
*論據
*
*如果{@code delimiter}或{@code elements},則@throws爲NullPointerException
*是{@code null}
*
*@請參見連接(CharSequence,CharSequence…)
*@看java.util.StringJoiner
*@從1.8開始
*/
public static String join(CharSequence delimiter,
Iterable<? extends CharSequence> elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
/**
*將此{@code String}中的所有字符轉換爲較低的
*案例使用給定{@code Locale}的規則。案例映射基於
*在由{@link指定的Unicode標準版本上java.lang.Character語言字符}
*上課。由於case映射並不總是1:1字符映射,因此
*{@code String}的長度可能不同於原始的{@code String}。
*<p>
*下表中列出了小寫映射的示例:
*<table border=“1”summary=“顯示語言環境、大寫、小寫和說明的語言代碼的小寫映射示例”>
*<tr>
*<th>語言環境代碼
*<th>大寫</th>
*<th>小寫</th>
*<th>說明
*</tr>
*<tr>
*<td>tr(土耳其語)</td>
*<td>\;u0130</td>
*<td>\;u0069</td>
*<td>上面帶點的大寫字母I->;小寫字母I</td>
*</tr>
*<tr>
*<td>tr(土耳其語)</td>
*<td>\;u0049</td>
*<td>\;u0131</td>
*<td>大寫字母I->;小寫字母無點I</td>
*</tr>
*<tr>
*<td>(全部)</td>
*<td>炸薯條</td>
*<td>炸薯條</td>
*<td>將字符串中的所有字符小寫</td>
*</tr>
*<tr>
*<td>(全部)</td>
*<td><img src=“文檔文件/卡比奧塔.gif“alt=”capiota“><img src=”文檔文件/卡奇.gif“alt=”卡奇“>
*<img src=“文檔文件/captheta.gif標題“alt=”captheta“><img src=”文檔文件/卷宗.gif“alt=”capupsil“>
*<img src=“文檔文件/辣椒.gif“alt=”辣椒“></td>
*<td><img src=“文檔文件/iota.gif網站“alt=”iota“><img src=”文檔文件/chi.gif語言“alt=”池“>
*<img src=“文檔文件/θ.gif“alt=”theta“><img src=”文檔文件/upsilon.gif網站“alt=”upsilon“>
*<img src=“文檔文件/sigma1.gif”alt=“sigma”></td>
*<td>將字符串中的所有字符小寫</td>
*</tr>
*</表格>
*
*@param locale使用此區域設置的大小寫轉換規則
*@返回{@code String},轉換爲小寫。
*@看java.lang.String#toLowerCase()
*@看java.lang.String#toUpperCase()
*@看java.lang.String#toUpperCase(區域設置)
*@從1.1開始
*/
public String toLowerCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstUpper;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
return this;
}
char[] result = new char[len];
int resultOffset = 0; /* result may grow, so i+resultOffset
*是結果中的寫入位置*/
/* Just copy the first few lowerCase characters. */
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for (int i = firstUpper; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE
&& (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent ||
srcChar == '\u03A3' || // GREEK CAPITAL LETTER SIGMA
srcChar == '\u0130') { // LATIN CAPITAL LETTER I WITH DOT ABOVE
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else {
lowerChar = Character.toLowerCase(srcChar);
}
if ((lowerChar == Character.ERROR)
|| (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (lowerChar == Character.ERROR) {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
} else if (srcCount == 2) {
resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
continue;
} else {
lowerCharArray = Character.toChars(lowerChar);
}
/* Grow result if needed */
int mapLen = lowerCharArray.length;
if (mapLen > srcCount) {
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)lowerChar;
}
}
return new String(result, 0, len + resultOffset);
}
/**
*將此{@code String}中的所有字符轉換爲較低的
*案例使用默認區域設置的規則。這相當於打電話
*{@代碼toLowerCase(語言環境.getDefault())}.
*<p>
*<b>注意:</b>此方法對區域設置敏感,可能會產生意外的
*如果用於要解釋區域設置的字符串,則返回結果
*獨立地。
*例如編程語言標識符、協議密鑰和HTML
*標籤。
*例如,土耳其語言環境中的{@code“TITLE”.toLowerCase()}
*返回{@code“t\u0131tle”},其中'\u0131'是
*拉丁文小寫字母無點I字符。
*若要獲得對區域設置不敏感的字符串的正確結果,請使用
*{@代碼toLowerCase(語言環境.ROOT)}.
*<p>
*@返回{@code String},轉換爲小寫。
*@看java.lang.String#toLowerCase(區域設置)
*/
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
/**
*將此{@code String}中的所有字符轉換爲大寫
*案例使用給定{@code Locale}的規則。案例映射基於
*在由{@link指定的Unicode標準版本上java.lang.Character語言字符}
*上課。由於case映射並不總是1:1字符映射,因此
*{@code String}的長度可能不同於原始的{@code String}。
*<p>
*下表列出了區分區域設置和1:M大小寫映射的示例。
*
*<table border=“1”summary=“區分區域設置和1:M大小寫映射的示例。顯示語言環境、小寫、大寫和說明的語言代碼。“>
*<tr>
*<th>語言環境代碼
*<th>小寫</th>
*<th>大寫</th>
*<th>說明
*</tr>
*<tr>
*<td>tr(土耳其語)</td>
*<td>\;u0069</td>
*<td>\;u0130</td>
*<td>小寫字母i->;上面帶點的大寫字母i</td>
*</tr>
*<tr>
*<td>tr(土耳其語)</td>
*<td>\;u0131</td>
*<td>\;u0049</td>
*<td>小寫字母無點i->;大寫字母i</td>
*</tr>
*<tr>
*<td>(全部)</td>
*<td>\;u00df</td>
*<td>\;u0053\;u0053</td>
*<td>小寫字母尖s->;兩個字母:SS</td>
*</tr>
*<tr>
*<td>(全部)</td>
*<td>Fahrvergnü;生成</td>
*<td>FAHRVERGNÜ;生成</td>
*<td></td>
*</tr>
*</表格>
*@param locale使用此區域設置的大小寫轉換規則
*@返回{@code String},轉換爲大寫。
*@看java.lang.String#toUpperCase()
*@看java.lang.String#toLowerCase()
*@看java.lang.String#toLowerCase(區域設置)
*@從1.1開始
*/
public String toUpperCase(Locale locale) {
if (locale == null) {
throw new NullPointerException();
}
int firstLower;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstLower = 0 ; firstLower < len; ) {
int c = (int)value[firstLower];
int srcCount;
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
c = codePointAt(firstLower);
srcCount = Character.charCount(c);
} else {
srcCount = 1;
}
int upperCaseChar = Character.toUpperCaseEx(c);
if ((upperCaseChar == Character.ERROR)
|| (c != upperCaseChar)) {
break scan;
}
firstLower += srcCount;
}
return this;
}
/* result may grow, so i+resultOffset is the write location in result */
int resultOffset = 0;
char[] result = new char[len]; /* may grow */
/* Just copy the first few upperCase characters. */
System.arraycopy(value, 0, result, 0, firstLower);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] upperCharArray;
int upperChar;
int srcChar;
int srcCount;
for (int i = firstLower; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&
(char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent) {
upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);
} else {
upperChar = Character.toUpperCaseEx(srcChar);
}
if ((upperChar == Character.ERROR)
|| (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (upperChar == Character.ERROR) {
if (localeDependent) {
upperCharArray =
ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);
} else {
upperCharArray = Character.toUpperCaseCharArray(srcChar);
}
} else if (srcCount == 2) {
resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;
continue;
} else {
upperCharArray = Character.toChars(upperChar);
}
/* Grow result if needed */
int mapLen = upperCharArray.length;
if (mapLen > srcCount) {
char[] result2 = new char[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = upperCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)upperChar;
}
}
return new String(result, 0, len + resultOffset);
}
/**
*將此{@code String}中的所有字符轉換爲大寫
*案例使用默認區域設置的規則。這種方法相當於
*{@CODETOUPPERCASE代碼(語言環境.getDefault())}.
*<p>
*<b>注意:</b>此方法對區域設置敏感,可能會產生意外的
*如果用於要解釋區域設置的字符串,則返回結果
*獨立地。
*例如編程語言標識符、協議密鑰和HTML
*標籤。
*例如,土耳其語言環境中的{@code“title”.toUpperCase()}
*返回{@code“T\u0130TLE”},其中'\u0130'是
*拉丁文大寫字母I隨以上點字符。
*若要獲得對區域設置不敏感的字符串的正確結果,請使用
*{@CODETOUPPERCASE代碼(語言環境.ROOT)}.
*<p>
*@返回{@code String},轉換爲大寫。
*@看java.lang.String#toUpperCase(區域設置)
*/
public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}
/**
*返回一個字符串,該字符串的值爲該字符串的前導和尾隨
*已刪除空白。
*<p>
*如果這個{@code String}對象表示一個空字符
*序列,或字符序列的第一個和最後一個字符
*由這個{@code String}對象表示的都有代碼
*大於{@code'\u0020'}(空格字符),則
*返回對此{@code String}對象的引用。
*<p>
*否則,如果沒有代碼大於
*{@code'\u0020'}在字符串中,然後
*{@code String}表示空字符串的對象是
*回來了。
*<p>
*否則,讓<i>k</i>作爲
*其代碼大於{@code'\u0020'}的字符串,並讓
*<i>m</i>是字符串中其代碼的最後一個字符的索引
*大於{@code'\u0020'}。{@code字符串}
*返回對象,表示此字符串的子字符串
*以索引<i>k</i>處的字符開頭,以
*索引<i>m</i>處的字符-即
*{@代碼this.子字符串(k,m+1)}。
*<p>
*此方法可用於從
*字符串的開頭和結尾。
*
*@返回一個字符串,其值爲該字符串,任何前導和尾隨爲白色
*已刪除空格,或者如果此字符串沒有前導或
*尾隨空白。
*/
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;
}
/**
*這個對象(已經是一個字符串!)是它自己返回的。
*
*@返回字符串本身。
*/
public String toString() {
return this;
}
/**
*將此字符串轉換爲新的字符數組。
*
*@返回新分配的字符數組,其長度爲
*其內容被初始化爲包含
*此字符串表示的字符序列。
*/
public char[] toCharArray() {
//不能使用數組.copyOf因爲類初始化順序問題
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
/**
*使用指定的格式字符串和
*爭論。
*
*<p>始終使用的語言環境是{@link返回的語言環境
* java.util.Locale#getDefault() 語言環境.getDefault()}.
*
*@param format
*A<a href=util/格式。HTML>>format string</a>
*
*@param參數
*格式中的格式說明符引用的參數
*弦。如果參數多於格式說明符,則
*忽略額外的參數。參數的數目是
*變量,可以爲零。參數的最大數目爲
*由定義的Java數組的最大維度限制
*<cite>Java&trade;虛擬機規範</cite>。
*對…的行爲
*{@code null}參數取決於
*ref=“../util”/Formatter.html語法“>轉換</a>。
*
*@投擲java.util.IllegalFormatException
*如果格式字符串包含非法語法,則
*與給定參數不兼容的說明符,
*給定格式字符串的參數不足,或其他
*非法條件。所有可能的規格
*格式錯誤,請參見
*href=”../util/Formatter.html詳細信息“>詳細信息
*格式化程序類規範。
*
*@返回格式化字符串
*
*@看java.util.Formatter格式
*@從1.5開始
*/
public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
/**
*返回使用指定區域設置的格式化字符串,格式字符串,
*還有爭論。
*
*@參數l
*{@link平原java.util.Locale要在期間應用的區域設置}
*格式化。如果{@code l}是{@code null},則不進行本地化
*已應用。
*
*@param format
*A<a href=util/格式。HTML>>format string</a>
*
*@param參數
*格式中的格式說明符引用的參數
*弦。如果參數多於格式說明符,則
*忽略額外的參數。參數的數目是
*變量,可以爲零。參數的最大數目爲
*由定義的Java數組的最大維度限制
*<cite>Java&trade;虛擬機規範</cite>。
*對…的行爲
*{@code null}參數取決於
*<a ref=“../util/Formatter.html語法“>轉換</a>。
*
*@投擲java.util.IllegalFormatException
*如果格式字符串包含非法語法,則
*與給定參數不兼容的說明符,
*給定格式字符串的參數不足,或其他
*非法條件。所有可能的規格
*格式錯誤,請參見
*href=”../util/Formatter.html詳細信息“>詳細信息
*格式化程序類規範
*
*@返回格式化字符串
*
*@看java.util.Formatter格式
*@從1.5開始
*/
public static String format(Locale l, String format, Object... args) {
return new Formatter(l).format(format, args).toString();
}
/**
*返回{@code Object}參數的字符串表示形式。
*
*@param obj一個{@code對象}。
*@return如果參數是{@code null},則字符串等於
*{@code“null”};否則,的值
*{@代碼目標字符串已返回。
*@看java.lang.ObjectétoString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
/**
*返回{@code char}數組的字符串表示形式
*爭論。複製字符數組的內容;隨後
*字符數組的修改不會影響返回的
*弦。
*
*@param data字符數組。
*@返回包含
*字符數組。
*/
public static String valueOf(char data[]) {
return new String(data);
}
/**
*返回的特定子數組的字符串表示形式
*{@code char}數組參數。
*<p>
*{@code offset}參數是第一個
*子陣的特徵。{@code count}參數
*指定子陣列的長度。子數組的內容
*複製;字符數組的後續修改不
*影響返回的字符串。
*
*@param data字符數組。
*@param offset子數組的初始偏移量。
*@param計算子數組的長度。
*@返回包含
*字符數組的指定子數組。
*如果{@code offset}是
*負數,或者{@code count}爲負數,或者
*{@code offset+count}大於
*{@代碼數據長度}.
*/
public static String valueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
/**
*相當於{@link#valueOf(char[],int,int)}。
*
*@param data字符數組。
*@param offset子數組的初始偏移量。
*@param計算子數組的長度。
*@返回包含
*字符數組的指定子數組。
*如果{@code offset}是
*負數,或者{@code count}爲負數,或者
*{@code offset+count}大於
*{@代碼數據長度}.
*/
public static String copyValueOf(char data[], int offset, int count) {
return new String(data, offset, count);
}
/**
*相當於{@link#valueOf(char[])}。
*
*@param data字符數組。
*@返回包含
*字符數組。
*/
public static String copyValueOf(char data[]) {
return new String(data);
}
/**
*返回{@code boolean}參數的字符串表示形式。
*
*@param b a{@code boolean}。
*@return如果參數是{@code true},則返回一個等於
*{@code“true”}返回;否則,返回一個等於
*{@code“false”}返回。
*/
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
/**
*返回{@code char}的字符串表示形式
*爭論。
*
*@param c a{@code char}。
*@返回長度爲{@code 1}的字符串,其中包含
*作爲它的單個字符,參數{@code c}。
*/
public static String valueOf(char c) {
char data[] = {c};
return new String(data, true);
}
/**
*返回{@code int}參數的字符串表示形式。
*<p>
*這個表示正是
*{@代碼整數.toString}一個參數的方法。
*
*@param i an{@code int}參數。
*@返回{@code int}參數的字符串表示形式。
*@看java.lang.Integer#toString(內景,內景)
*/
public static String valueOf(int i) {
return Integer.toString(i);
}
/**
*返回{@code long}參數的字符串表示形式。
*<p>
*這個表示正是
*{@代碼長管柱}一個參數的方法。
*
*@param l a{@code long}參數。
*@返回{@code long}參數的字符串表示形式。
*@看java.lang.Long#toString(長)
*/
public static String valueOf(long l) {
return Long.toString(l);
}
/**
*返回{@code float}參數的字符串表示形式。
*<p>
*這個表示正是
*{@代碼浮動管柱}一個參數的方法。
*
*@param f a{@code float}。
*@返回{@code float}參數的字符串表示形式。
*@看java.lang.Float#toString(浮動)
*/
public static String valueOf(float f) {
return Float.toString(f);
}
/**
*返回{@code double}參數的字符串表示形式。
*<p>
*這個表示正是
*{@代碼雙弦}一個參數的方法。
*
*@param d a{@code double}參數。
*@返回{@code double}參數的字符串表示形式。
*@看java.lang.Double字符串(雙倍)
*/
public static String valueOf(double d) {
return Double.toString(d);
}
/**
*返回字符串對象的規範表示形式。
*<p>
*字符串池最初是空的,由
*類{@code String}。
*<p>
*當調用intern方法時,如果池已包含
*字符串等於此{@code string}對象,由
*{@link#equals(Object)}方法,則池中的字符串是
*回來了。否則,此{@code String}對象將添加到
*返回池和對此{@code String}對象的引用。
*<p>
*因此,對於任意兩個字符串{@code s}和{@code t},
*{@code s.intern()==t.intern()}是{@code true}
*如果且僅當{@code s.equals(t)}是{@code true}。
*<p>
*所有文本字符串和字符串值常量表達式都是
*實習。字符串文本在
*<cite>Java&trade;語言規範。
*
*@返回一個與此字符串內容相同的字符串,但是
*保證來自唯一字符串池。
*/
public native String intern();
}
jdk1.8中文註釋版源碼下載 (附實例)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.