Java StringBuffer getChars()方法及示例
StringBuffer类 的 getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 方法从实际序列中复制从index:srcBegin到index:srcEnd-1的字符到一个作为参数传递给函数的char数组中。
- 这些字符被复制到char dst[]数组中,从index:dstBegin开始,到index:dstbegin+(srcEnd-srcBegin)-1结束。
- 第一个被复制到数组中的字符位于索引 srcBegin,最后一个被复制的字符位于索引 srcEnd-1。
- 要复制的字符总数等于srcEnd-srcBegin。
语法
参数: 本方法需要四个参数。
- srcBegin :int值,代表要开始复制的索引。
- srcEnd :int值,代表要停止复制的索引。
- dst :char数组,表示要将数据复制到的数组。
- dstBegin :int值,代表开始粘贴复制数据的dst数组的索引。
返回: 该方法不返回任何东西。
异常: 该方法会抛出StringIndexOutOfBoundsException,如果。
- srcBegin < 0
- dstBegin < 0
- srcBegin > srcEnd
- srcEnd > this.length()
- dstBegin+srcEnd-srcBegin > dst.length
下面的程序演示了StringBuffer类的getChars()方法:
例1 :
输出
例2: 演示StringIndexOutOfBoundsException。
输出
**参考文献: ** https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#getChars(int, int, char%5B%5D, int)