How to copy the characters of a given String into an Array of characters using getChars() method

preview_player
Показать описание
How to copy the characters of a given String into an Array of characters using the Java getChars() method

In this video, we will talk about the getChars() method, which is useful when copying characters of a given String into an Array of characters.
Now, let’s take an example to show you how this method works

String str = “I love programming”;
We will need an Array in which we will store all the copied characters

char[] copied_characters = new char[10];
Now, let’s call out method

This method takes four parameters:
• the first parameter is of type integer and it represents the index of the first character in the String to copy
• the second parameter is of type integer and it represents the index of the last character in the String to copy
• the third parameter represents the array of characters in which the characters from the String get copied
• the fourth parameter represents the index position in the Array from where the copied characters will be pushed into array


Let’s print the content of our array
for(char content : copied_characters) {
}

Let’s also note that, if
• The first parameter if less than zero, the method will throw an exception
• The second parameter is greater than the length of the String, the method will throw an exception
• The third parameter is less than zero, the method will throw an exception
• The fourth parameter is greater than the length of the Array, the method will throw an exception

#codingriver
#java
#programming
Рекомендации по теме