Here we have a small class that convert a string literal into array, a character array. To do this we can simply used String.toCharArray()
method.
package org.kodejava.example.lang;
public class StringToArrayExample {
public static void main(String[] args) {
// We have a string literal that contains the motto of this website.
String literal = "Kode Java - Learn Java Programming by Examples";
// Now we want to convert or divided it into a small array of char.
// To do this we can simply used String.toCharArray() method. This
// method splits the string into an array of characters.
char[] temp = literal.toCharArray();
// Here we just iterate the char array and print it to our console.
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i]);
}
}
}
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020