Java String.getChars()方法用法实例教程,从这个字符串中的字符复制到目标字符数组
描述
java.lang.String.getChars() 方法从这个字符串中的字符复制到目标字符数组
要复制的第一个字符在索引srcBegin处,被复制的最后一个字符是在的索引srcEnd1即要复制的字符总数是srcEnd srcBegin处。
字符被复制到子数组的夏令时开始在指数dstBegin和结束于索引:dstbegin + (srcEnd-srcBegin) - 1
声明
以下是声明java.lang.String.getChars()方法
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
参数
-
srcBegin -- This is the index of the first character in the string to copy.
-
srcEnd -- This is the index after the last character in the string to copy.
-
dst -- This is the destination array.
-
dstBegin -- This is the start offset in the destination array.
返回值
此方法不返回任何值
异常
-
IndexOutOfBoundsException -- 如果以下是true,它抛出这个异常:
srcBegin is negative srcBegin is greater than srcEnd srcEnd is greater than the length of this string dstBegin is negative dstBegin+(srcEnd-srcBegin) is larger than dst.length
实例
下面的例子显示使用的java.lang.String.getChars()方法
package com.yiibai; import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "Website:www.yiibai.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.yiibai.com Value of character array : yiibai