共同学习Java源代码--常用工具类--AbstractStringBuilder(二)
2016-04-18 14:57
447 查看
public void setLength(int newLength) {
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
ensureCapacityInternal(newLength);
if (count < newLength) {
Arrays.fill(value, count, newLength, '\0');
}
count = newLength;
}
这个方法是设置可变字符串长度的方法。先判断参数,也就是要设置的长度是否小于零,小于零的话抛出异常。然后调用之前的方法扩大底层数组的长度。然后判断如果已有的字符数小于参数,那么就将扩容后的char数组value从count位到参数所在位置都填满\0也就是空字符。
public char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
这个方法比较简单就是返回指定下标的字符。
public int codePointAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, count);
}
这个方法是返回指定下标的代码点。
public int codePointBefore(int index) {
int i = index - 1;
if ((i < 0) || (i >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointBeforeImpl(value, index, 0);
}
这是获取该元素之前那个元素的代码点。
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
}
这是获取指定下标区间内代码点的数量。
public int offsetByCodePoints(int index, int codePointOffset) {
if (index < 0 || index > count) {
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0, count,
index, codePointOffset);
}
这个方法是返回从String对象指定索引开始,到另一个指定索引结束的区间内的代码点长度。
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
ensureCapacityInternal(newLength);
if (count < newLength) {
Arrays.fill(value, count, newLength, '\0');
}
count = newLength;
}
这个方法是设置可变字符串长度的方法。先判断参数,也就是要设置的长度是否小于零,小于零的话抛出异常。然后调用之前的方法扩大底层数组的长度。然后判断如果已有的字符数小于参数,那么就将扩容后的char数组value从count位到参数所在位置都填满\0也就是空字符。
public char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
这个方法比较简单就是返回指定下标的字符。
public int codePointAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointAtImpl(value, index, count);
}
这个方法是返回指定下标的代码点。
public int codePointBefore(int index) {
int i = index - 1;
if ((i < 0) || (i >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return Character.codePointBeforeImpl(value, index, 0);
}
这是获取该元素之前那个元素的代码点。
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
}
这是获取指定下标区间内代码点的数量。
public int offsetByCodePoints(int index, int codePointOffset) {
if (index < 0 || index > count) {
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0, count,
index, codePointOffset);
}
这个方法是返回从String对象指定索引开始,到另一个指定索引结束的区间内的代码点长度。
相关文章推荐
- hdu 5667 Sequence
- require 书写约定
- Getting started with caffe questions answers (摘选)
- java easyui自定义拓展组件
- 替换UISearchBar的取消按钮
- iOS开发之指定UIView的某几个角为圆角
- String、StringBuffer、StringBuilder的区别
- Swifit指纹识别小Demo
- IOS Quartz2D 通过UIColor生成图片
- UITableView优化技巧
- 解决通过UIAlertView按钮点击事件pop回上级页面键盘会响应问题
- EasyUI tree 相关
- Sketch真的很难上手?
- [Android实例] Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- iOS - UISearchController搜索框与NSPredicate谓词
- identityHashCode, WeakReference,ReferenceQueue和Lucene WeakIdentityMap
- UITableView的常用代理方法
- AVI Player with Playmaker actions 使用UGUI显示视频以及控制播放视频 (二)
- 理解 Android Build 系统
- AndroidUI--SlidingMenu使用例子