Java 中的getChars()方法

目录
文章目录隐藏
  1. 语法
  2. 参数
  3. 返回值
  4. 实例

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.mybj123.com");
        char[] Str2 = new char[7];

        try {
            Str1.getChars(4, 11, Str2, 0);
            System.out.print("拷贝的字符串为:" );
            System.out.println(Str2 );
        } catch( Exception ex) {
            System.out.println("触发异常...");
        }
    }
}

以上程序执行结果为:

拷贝的字符串为:mybj123

「点点赞赏,手留余香」

0

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » Java 中的getChars()方法

发表回复