Java String.getChars()方法

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
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值