Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

getChars()

Example

Copy part of a string into a char array:

char[] myArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
System.out.println(myArray);
String myStr = "Hello, World!";
myStr.getChars(7, 12, myArray, 4);
System.out.println(myArray);

Definition and Usage

The getChars() method transfers characters from a string to a char array.

Syntax

public void getChars(int start, int end, char[] destination, int position)

Parameter Values

Parameter

Description

start

“This parameter is mandatory. It indicates the position in the string where the copying of characters should start.”

end

“This parameter is mandatory. It indicates the position in the string after the last character to be copied.”

destination

“This parameter is mandatory. It refers to the array to which the characters will be copied.”

position

“This parameter is mandatory. It indicates the position in the destination array where the copied characters will be written.”

Technical Details

 

Return:

None

Throws:

IndexOutOfBoundsException may occur under these conditions:

 

  • If start or position are negative.
  • If end exceeds the length of the string.
  • If start is greater than end.
  • If the copied characters cannot fit into the destination array.

Java version:

Any