String to char array Java
In this post, you will learn how to convert string to char array using Java programming language.
In Java, string is basically an object that represents sequence of char values. For example, "welcome" is a string containing a sequence of characters 'w' , 'e' , 'l' , 'c' , 'o' , 'm' and 'e' .
The Java char keyword is a primitive data type. It is used to declare the character-type variables and methods.
Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location.
Convert string to char array using Naive Approach
In the given example, first we take a string and create a character array of the same length as of string. Next, we traverse over the string to copy character at the i'th index of string to the i'th index in the array and display the array element. For this, we use the charAt() method. The charAt() is an instance method of the String class. It returns a character at the specified index of the current string.
import java.util.*;
public class StringCharArray {
public static void main(String args[])
{
String str = "etutorialspoint";
// Creating array of string length
char[] chr = new char[str.length()];
// Copying character by character into array
for(int i = 0; i < str.length(); i++) {
chr[i] = str.charAt(i);
}
// Printing array
System.out.println("Char Array elements: ");
for(char ch : chr) {
System.out.println(ch);
}
}
}
Output of the above code:
Char Array elements:
e
t
u
t
o
r
i
a
l
s
p
o
i
n
t
Convert string to char array using toCharArray() Approach
The toCharArray() method in Java converts the given string to a new character array. It returns a newly allocated character array, whose length is the length of this string and whose contents are initialized to contain the character sequence represented by the string.
import java.util.*;
public class StringCharArray {
public static void main(String args[])
{
String str = "welcometoetp";
// Storing the array
// returned by toCharArray()
char[] chr = str.toCharArray();
// Displaying array
System.out.println("Char Array elements: ");
for (char ch : chr) {
System.out.println(ch);
}
}
}
Output of the above code:
Char Array elements:
w
e
l
c
o
m
e
t
o
e
t
p
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java