GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - String toCharArray() Method
Selected Reading
Java - String toCharArray() MethodThe Java String toCharArray() method is used to convert the current string to a character array. This method returns a newly formed character array with a length equal to the given string and with the characters in the given string initialized as its contents. For example, the string Tutorials Point is converted into a new character array Tutorials Point where the size of the character is 15 (spaces are included). Note − Unlike C, in Java, a character array is distinct from a string array and the NULL character cannot terminate either a string or a character array. SyntaxFollowing is the syntax for Java String toCharArray() method − public char[] toCharArray() ParametersThis method does not accept any parameter. Return ValueThis method 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 this string. Getting Char Array From a String ExampleThe following example shows the usage of Java String toCharArray() method by printing the character array of the given string value " Java was developed by James Gosling" − package com.tutorialspoint;
public class StringDemo {
public static void main(String[] args) {
// converts String value to character array type value
String str = " Java was developed by James Gosling";
char retval[] = str.toCharArray();
// displays the converted value
System.out.println("Converted value to character array = ");
System.out.println(retval);
}
}
OutputIf you compile and run the above program, it will produce the following result − Converted value to character array = Java was developed by James Gosling Getting Char Array and its Size From a String ExampleFollowing is an example to print the size of the character array of the given string "Tutorials point!" using toCharArray() method − package com.tutorialspoint;
public class StringDemo {
public static void main(String[] args) {
String s = new String("Tutorials point!");
char[] array = s.toCharArray();
System.out.print("The string i:");
System.out.println(s.toCharArray());
System.out.println("Size of char is: " + array.length);
}
}
OutputIf you compile and run the program above, the output will be displayed as follows − The string i:Tutorials point! Size of char is: 16 Getting Char Array From a String ExampleLet's examine one more char array example. Without having to write any custom code, it is a useful method that returns a char array from the string − package com.tutorialspoint;
public class StringDemo {
public static void main(String[] args) {
String s = "Programming";
char[] c = s.toCharArray();
System.out.println("Size of char array: " + c.length);
System.out.println("Elements of character array: ");
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
}
}
OutputOn executing the program above, the output is obtained as follows − Size of char array: 11 Elements of character array: P r o g r a m m i n g Getting Char Array From a String ExampleBelow is the code to return the character array of the string value passed as an argument by iterating over the string through a loop − package com.tutorialspoint;
public class StringDemo {
public static void main(String args[]) {
String s = "Tutorials Point";
char[] c = s.toCharArray();
int l = c.length;
for (int i = 0; i < l; i++) {
System.out.print(c[i]);
}
}
}
OutputThe output of the above program is as follows − Tutorials Point java_lang_string.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |