Example of getChars(int startIndex, int endIndex, char[] desArray, int offest) method of String class.
Example of getChars(int startIndex, int endIndex, char[] desArray, int offest) method of String class.
In this java example, we will explain about the getChars(int startIndex, int endIndex, char[] desArray, int offest) method of String class in java. The getChars(..) method copies the string content into destination character array. It has four argument startIndex, endIndex, desArray, offest.
startIndex-- Starting index of character. Which you wants to
copy into destination character array.
endIndex-- Last index of character. which you wants to copy
into destination array. Last character(endIndex-1)
desArray-- Destination character array.
offest-- Starting index of destination array.
Code:
GetCharsMethod.java
public class GetCharsMethod { public static void main(String[] arr) { // Declare a string String sourceString = "Rose India Technology"; // Display value of string System.out.println("Given string : " + sourceString); // Declare a character array char[] destCharacterArray = new char[5]; sourceString.getChars(5, 10, destCharacterArray, 0); System.out.println("Element in destination character array."); for (int i = 0; i < destCharacterArray.length; i++) { System.out.print(destCharacterArray[i] + " "); } } }Output:
CGiven string : Rose India Technology
Element in destination character array. I n d i a |
[ 0 ] Comments