共同学习Java源代码--常用工具类--AbstractStringBuilder(六)
来源:互联网 发布:办公用品库存软件 编辑:程序博客网 时间:2024/06/10 02:52
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
System.arraycopy(value, index+1, value, index, count-index-1);
count--;
return this;
}
这个方法是删除指定下标的字符。
先判断下标是否越界,否则抛异常。
然后将下标之后的元素拷贝到下标处,count自减一。最后返回本对象。
public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > end)
throw new StringIndexOutOfBoundsException("start > end");
if (end > count)
end = count;
int len = str.length();
int newCount = count + len - (end - start);
ensureCapacityInternal(newCount);
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(value, start);
count = newCount;
return this;
}
这个方法是将一个字符串镶嵌在本对象中。
先给value扩容,扩大的容量就是两个字符串之和减去下标之差。
然后将end下标之后的字符复制到start+len下标之后,这样start和end之间就有了len长度的空白,然后将参数填充进去。
最后返回本对象。
public String substring(int start) {
return substring(start, count);
}
public CharSequence subSequence(int start, int end) {
return substring(start, end);
}
public String substring(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
throw new StringIndexOutOfBoundsException(end);
if (start > end)
throw new StringIndexOutOfBoundsException(end - start);
return new String(value, start, end - start);
}
这三个方法一起看,就是截取字符串。这里是返回新字符串。
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(六)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(一)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(二)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(三)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(四)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(五)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(七)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(八)
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(九)
- 共同学习Java源代码--常用工具类--StringBuffer(一)
- 共同学习Java源代码--常用工具类--StringBuffer(二)
- 共同学习Java源代码--常用工具类--StringBuffer(三)
- 共同学习Java源代码--常用工具类--StringBuffer(四)
- 共同学习Java源代码--数据结构--AbstractList抽象类(六)
- 共同学习Java源代码--数据结构--ArrayList类(六)
- 共同学习Java源代码--数据结构--LinkedList类(六)
- 共同学习Java源代码-数据结构-HashMap(六)
- 共同学习Java源代码--数据结构--AbstractSequentialList类
- iPhone之为UIView设置阴影(CALayer的shadowColor,shadowOffset,shadowOpacity,shadowRadius,shadowPath属性)
- 基于TeXlive,使用Sublime Text 3编写LaTeX
- HashMap源码之hash()函数分析(JDK 1.8)
- 【neutron】mitaka版本openstack网络之open vSwitch
- UVA_102_Ecological Bin Packing
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(六)
- 电子商务EC模式
- Gradle 笔记——Java构建入门
- (SDL)(01)SDL Introduction
- 在糖尿病患者信息管理系统中,交流区模块想模仿论坛那样几楼几楼的!
- C++各种数据类型之间的转换
- 开源CORBA OmniORB开发环境搭建及编译问题解决方法
- 贪心算法☞会场安排问题/图着色
- c++ map使用