toCharArray
public char[] toCharArray()
- 将此字符串转换为一个新的字符数组。
-
返回:
- 一个新分配的字符数组,它的长度是此字符串的长度,而且内容被初始化为包含此字符串表示的字符序列。
例:
public class Program
{
public
static void main(String[] args)
{
String str = "This is a String.";
// Convert the above string to a char array.
char[] arr = str.toCharArray();
// Display the contents of the char array.
System.out.println(arr);
}
}
public class ToCharArrayString {
public static void main(String args[]) {
//method
converts complete String value to char array type
value
String str =
" einstein relativity concept is still a concept of great
discussion";
char heram[]
= str.toCharArray();
// complete
String str value is been converted in to char array data by
// the
method
System.out.print("Converted value from String to char array
is: ");
System.out.println(heram);
}
}
Output of the program:-
Converted value from String to char array
is: einstein relativity concept
is still a concept of great discussion