Java String getChars()方法

Java String getChars()方法将此字符串中的字符复制到目标字符数组中。

语法

以下是此方法的语法 -

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

参数

  • srcBegin - 要复制的字符串中第一个字符的索引。
  • srcEnd - 要复制的字符串中最后一个字符后面的索引。
  • dst - 目标数组。
  • dstBegin - 目标数组中的起始偏移量。

返回值

  • 它不返回任何值,但可能会抛出IndexOutOfBoundsException

示例


import java.io.*;

public class Test {

    public static void main(String args[]) {
        String Str1 = new String("Welcome to xuhuhu.com");
        char[] Str2 = new char[7];
        try {
            Str1.getChars(8, 15, Str2, 0);
            System.out.print("Copied Value = ");
            System.out.println(Str2);
        } catch (Exception ex) {
            System.out.println("Raised exception...");
        }
    }
}

执行上面示例代码,得到以下结果:

Copied Value = to Yiib

上一篇: Java String类 下一篇: Java快速入门