Java getChars() 方法

Java String类Java  String类


getChars() 方法将字符从字符串复制到目标字符数组。

语法

public void getChars(int srcBegin, int srcEnd, char[] dst,  int dstBegin)

参数

  • srcBegin -- 字符串中要复制的第一个字符的索引。

  • srcEnd -- 字符串中要复制的最后一个字符之后的索引。

  • dst -- 目标数组。

  • dstBegin -- 目标数组中的起始偏移量。

返回值

没有返回值,但会抛出 IndexOutOfBoundsException 异常。

实例

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("www.facesoho.com");
        char[] Str2 = new char[9];

        try {
            Str1.getChars(4, 12, Str2, 0);
            System.out.print("copied string is:" );
            System.out.println(Str2 );
        } catch( Exception ex) {
            System.out.println("Exception ...");
        }
    }
}

执行结果 可以看出是第一个字符是位置是1 而不是0开始

copied string is:facesoho

Java String类Java  String类