String getChar()

This method is used to copy the current string into a character array. You must provide the char array to the method in which you want to copy the content current string.

public void getChars(int srcStartIndex, int srcEndIndex, char[] destination, int dstStartIndex)  

This method doesn’t return anything, Its return type is void. This method takes four parameters we will discuss it.

int srcStartIndex: It is the start index value of the current string that we want to copy. It means the start index is the location of the first character in the string to copy.
int srcEndIndex: It is the end index value of the current string that we want to copy. It means the end index is the location of the last character in the string to copy.
char[] destination: It is a char array where the characters will get copied.
int dstStartIndex: It is the start index value of char array(destination). It means it the index of the array starting from where the characters will be pushed into the array.

stringName.getChar(startIndex, endIndex, destinationArray, startDesinationIndex)
class ExampleOfGetChar
 {  
	 public static void main(String args[])
	 {    
		String name = "JAVA WINGS";  
		char nameArray[] = new char[6];
		// Copy the word JAVA in char array
		name.getChars(0, 4, nameArray, 0);
		System.out.println(nameArray);
		 
		// Copy the word WINGS in char array
		 name.getChars(5, 10, nameArray, 0);
		 System.out.println(nameArray); 
	}
}  

Output: JAVA
WINGS

Leave a Comment