How to convert a String into an Array of characters using the toCharArray() method
How to convert a String into an Array of characters using the toCharArray() method
In this video, we will talk discuss on the Java toCharArray() method, which is used to convert a String into a sequence of characters and it returns an array of characters.
We can also note that the length of the array of characters returned is equal to the length of the given String and the sequence of characters in the Array matches the sequence of characters in the String.
Let’s take an example to see how this method works
String str = “I love programming”;
Since we know that the method will return an array of characters then we have to declare an array that will store the value returned by the method
char[] array = str.toCharAray();
Now, in order to print the content of our array, we will write this
for(char string_characters : array) {
System.out.println(“Here are the characters”);
System.out.println(string_characters);
}
We could even choose to print each character in the array based on their index position, by writing this
System.out.println(array[5]);
Or do a Arrays.toString() method, like this
System.out.println(Arrays.toString(array));
#codingriver
#java
#programming
Видео How to convert a String into an Array of characters using the toCharArray() method автора Умные технологии
Видео How to convert a String into an Array of characters using the toCharArray() method автора Умные технологии
Информация
2 декабря 2023 г. 4:24:39
00:05:51
Похожие видео