String to Char Array

String to Char array conversion: Yes, we can do it. It is dead simple. It is just possible with a simple method String class' toCharArray() which converts string to char array easily. It converts that string to char array on which the toCharArray() method is called.


class StringToCharArray
{
public static void main(String args[])
{

// Create a string
String st="abcdefghijklmnopqrstuvwxyz";

// Convert string to char array
char[] c=st.toCharArray();

// Print char array
for(int i=0;i<c.length;i++)
{
System.out.println(c[i]);
}
}
}
st.toCharArray(): This method as is called on the string object st which is pre-stored in the class string when the object was created, converts that object into character array.
The internal coding for this method may be splitting the given string with "" that is it, if split() method has come before this method has. Whatever, you can also work the same with the split() method in the String class by passing the "" to split() method.

Here is an example of split method, you can refer it here.

-----------------------------------
Output
-----------------------------------

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z


Posted by at on

Tags: java.lang, String Handling,