Returns a canonical representation for the string object.*<p>* A pool of strings, initially empty, is maintained privately by the
*class{@code String}.*<p>* When the intern method is invoked,if the pool already contains a
* string equal to this{@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise,this{@code String} object is added to the
* pool and a reference to this{@code String} object is returned.*<p>* It follows that for any two strings {@code s} and {@code t},*{@code s.intern()== t.intern()} is {@codetrue}*if and only if{@code s.equals(t)} is {@codetrue}.*<p>* All literal strings and string-valued constant expressions are
* interned. String literals are defined in section 3.10.5 of the
*<cite>The Java™ Language Specification</cite>.** @return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.*/publicnative String intern();
publicString(int[] codePoints,int offset,int count){if(offset <0){thrownewStringIndexOutOfBoundsException(offset);}if(count <=0){if(count <0){thrownewStringIndexOutOfBoundsException(count);}if(offset <= codePoints.length){this.value ="".value;return;}}// Note: offset or count might be near -1>>>1.if(offset > codePoints.length - count){thrownewStringIndexOutOfBoundsException(offset + count);}finalint end = offset + count;// Pass 1: Compute precise size of char[]//计算扩展字符数量,字符长度为count+扩展字符数量 int n = count;for(int i = offset; i < end; i++){int c = codePoints[i];if(Character.isBmpCodePoint(c))continue;elseif(Character.isValidCodePoint(c))
n++;elsethrownewIllegalArgumentException(Integer.toString(c));}// Pass 2: Allocate and fill in char[]finalchar[] v =newchar[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;}
III.String提供了查找目标字符或字符串在某个字符串中的索引号,其中比较特别的是LastIndexOf(char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex)方法,也就是从尾查找目标字符串在指定字符串中的结束索引号,特别之处在于如果目标字符串为空字符串时返回指定字符串的结束索引号,这里应该使用了集合概念,空集是任意集合的子集。
//代码清单:staticintlastIndexOf(char[] source,int sourceOffset,int sourceCount,char[] target,int targetOffset,int targetCount,int fromIndex){/*
* Check arguments; return immediately where possible. For
* consistency, don't check for null 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;}}