getChars()
getChars() 方法将字符从字符串复制到目标字符数组。
语法
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
参数
- srcBegin – 字符串中要复制的第一个字符的索引。
- srcEnd – 字符串中要复制的最后一个字符之后的索引。
- dst – 目标数组。
- dstBegin – 目标数组中的起始偏移量。
返回值
没有返回值
实例
public class Test {
public static void main(String args[]) {
String str1 = new String("Hello world");
char[] str2 = new char[6];
str1.getChars(4, 10, str2, 0);
System.out.println(str2 );
}
}
以上程序执行结果为:
o worl
源码
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}//不在数据范围内,抛异常
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
底层采用的是数组