实现了Appendable和CharSequence
implements Appendable, CharSequence
1.codePointCount(int beginIndex, int endIndex)
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
}
String.length() 返回字符串所使用的代码单元个数,或者说返回字符串占用的char类型数据个数。String.codePointCount() 则返回字符串的码点个数,一个字符对应一个码点,所以该方法可以真正返回字符串当中的字符个数。length()方法和codePointCount()方法在某些情况下返回值相等,即字符串当中字符都是常见字符时(码点小于等于0xFFFF)
2.append(String str)
public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);//扩容
str.getChars(0, len, value, count);//复制追加str
count += len;//增加长度
return this;
}
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0) {//如果新值长度minimumCapacity大于当前数组长度,就new 一个更大的数组,并拷贝值
value = Arrays.copyOf(value,
newCapacity(minimumCapacity));
}
}
private int newCapacity(int minCapacity) {//fan
// overflow-conscious code
int newCapacity = (value.length << 1) + 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
? hugeCapacity(minCapacity)
: newCapacity;
}
private int hugeCapacity(int minCapacity) {
if (Integer.MAX_VALUE - minCapacity < 0) { // overflow
throw new OutOfMemoryError();
}
return (minCapacity > MAX_ARRAY_SIZE)
? minCapacity : MAX_ARRAY_SIZE;
}
3.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
srcBegin -- 字符串中要复制的第一个字符的索引。
srcEnd -- 字符串中要复制的最后一个字符之后的索引。
dst -- 目标数组。
dstBegin -- 目标数组中的起始偏移量。
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
{
if (srcBegin < 0)
throw new StringIndexOutOfBoundsException(srcBegin);
if ((srcEnd < 0) || (srcEnd > count))
throw new StringIndexOutOfBoundsException(srcEnd);
if (srcBegin > srcEnd)
throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
public static void main(String args[]) {
String Str1 = new String("www.runoob.com");
char[] Str2 = new char[6];
try {
Str1.getChars(4, 10, Str2, 0);
System.out.print("拷贝的字符串为:" );
System.out.println(Str2 );
} catch( Exception ex) {
System.out.println("触发异常...");
}
}
拷贝的字符串为:runoob
4.arraycopy(很多字符修改拷贝的操作都是调用这个方法)
src 原对象
srcPos 原对象复制的起始位置
dest 目标对象
destPos 目标对象替换的起始位置
length 替换的长度
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
public static void main(String args[]) {
char[] c1 = new String("123456").toCharArray();
char[] c2 = new String("abcdef").toCharArray();
System.arraycopy(c1,1 , c2, 1, 2);
for(char c : c1){
System.out.print(c);
}
System.out.println();
for(char c : c2){
System.out.print(c);
}
}
输出
123456
a23def
5.reverse()
将内容全部反转
public AbstractStringBuilder reverse() {
boolean hasSurrogates = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; j--) {
int k = n - j;
char cj = value[j];
char ck = value[k];
value[j] = ck;
value[k] = cj;
if (Character.isSurrogate(cj) ||
Character.isSurrogate(ck)) {
hasSurrogates = true;
}
}
if (hasSurrogates) {
reverseAllValidSurrogatePairs();
}
return this;
}
/** Outlined helper method for reverse() */
private void reverseAllValidSurrogatePairs() {
for (int i = 0; i < count - 1; i++) {
char c2 = value[i];
if (Character.isLowSurrogate(c2)) {
char c1 = value[i + 1];
if (Character.isHighSurrogate(c1)) {
value[i++] = c1;
value[i] = c2;
}
}
}
}
网友评论