當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.String.getChars()用法及代碼示例


描述

這個java.lang.String.getChars() 方法將此字符串中的字符複製到目標字符數組中。

要複製的第一個字符位於索引處srcBegin,要複製的最後一個字符在索引處srcEnd-1即要複製的字符總數是srcEnd-srcBegin

字符被複製到子數組中dst從索引開始dstBegin並在索引處結束:dstbegin + (srcEnd-srcBegin) - 1

聲明

以下是聲明java.lang.String.getChars()方法

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

參數

  • srcBegin- 這是要複製的字符串中第一個字符的索引。

  • srcEnd- 這是要複製的字符串中最後一個字符之後的索引。

  • dst- 這是目標數組。

  • dstBegin- 這是目標數組中的起始偏移量。

返回值

此方法不返回任何值。

異常

IndexOutOfBoundsException- 如果以下任何一項為真,則拋出此異常 -

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.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str = "Website:www.tutorialspoint.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.tutorialspoint.com
Value of character array:tutorialspoint

相關用法


注:本文由純淨天空篩選整理自 Java.lang.String.getChars() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。