Java.lang.String.getChars() 方法
描述
java.lang.String.getChars() 方法将此字符串中的字符复制到目标字符数组中。
要复制的第一个字符在索引 srcBegin 处,要复制的最后一个字符在索引 srcEnd-1 处,即要复制的字符总数是 srcEnd-srcBegin。
字符被复制到 dst 的子数组中,从索引 dstBegin 开始,到索引结束:dstbegin + (srcEnd-srcBegin) - 1
声明
以下是 java.lang.String.getChars() 方法的声明。
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
参数
srcBegin − 这是要复制的字符串中第一个字符的索引。
srcEnd − 这是要复制的字符串中最后一个字符之后的索引。
dst − 这是目标数组。
dstBegin − 这是目标数组中的起始偏移量。
返回值
此方法不返回任何值。
异常
IndexOutOfBoundsException − 如果以下任何一项为真,则抛出此异常 −
srcBegin 是负数 srcBegin 大于 srcEnd srcEnd 大于此字符串的长度 dstBegin 为负 dstBegin+(srcEnd-srcBegin) 大于 dst.length
示例
下面的例子展示了 java.lang.String.getChars() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str = "Website:www.tutorialspoint.com";
System.out.println(str);
// character array
char[] chararr = new char[30];
/* copies characters from starting and ending index into the destination
character array */
str.getChars(12, 26, chararr, 0);
// print the character array
System.out.print("Value of character array : ");
System.out.println(chararr);
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Website:www.tutorialspoint.com Value of character array : tutorialspoint

