菜鸟教程 -- 学的不仅是技术,更是梦想!

Java 教程
(追記) (追記ここまで)

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.runoob.com");
    char[] Str2 = new char[6];
    try {
      Str1.getChars(4, 10, Str2, 0);
      System.out.print("拷贝的字符串为:" );
      System.out.println(Str2 );
    } catch( Exception ex) {
      System.out.println("触发异常...");
    }
  }
}

以上程序执行结果为:

拷贝的字符串为:runoob

Java String类Java String类

AI 思考中...

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /