1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* Convert String to Character Array Example This example shows how to convert a given String object to an array of character */ public class StringToCharacterArrayExample { public static void main(String args[]){ //declare the original String object String strOrig = "Hello World"; //declare the char array char[] stringArray; //convert string into array using toCharArray() method of string class stringArray = strOrig.toCharArray(); //display the array for(int index=0; index < stringArray.length; index++) System.out.print(stringArray[index]); } } /* Output of the program would be : Hello World */ |
Thank you. Your code snippet is very useful.
thank you very, much it helped me constructing something similar to c atoi in java.
How do you convert a String to a 2D character array?
you have to use nested loops, like:
for (int i = 0; i < “the row”.length; i++) {
for (int j = 0; i < “the column”.length; j++) {
System.out.println(“the matrix”[i][j]);
}
}
Its great code..
Rahul
thanks a lot buddy……
Thanks! Very useful for beginners like me.
Thank you veru much your code is very helpful for me.
thanks man
this example just saved many many hours of searching. Thank you making it so easy to read!!!!
Ali (Iran-Zanjan) :
Very Very Thanks
i need the string to character conversion with out using toCharArray(); method and also with out using any api;
plz help me any one
you can use charAt() method
Did u got the Solution for that prblem pls reply me
i need the string to character conversion with out using toCharArray(); method and also with out using any api;
sir,
I want to know about a code..
if i enter a name as “Amitabh Bacchan”, it should return a value “A Bacchan”..
How can i make this program.???
import java.util.Scanner;
public class StringToCharacterArrayExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“enter you name “);
String name = sc.nextLine();
String[] namearr = name.split(” “);
String name1 = (namearr[0]);
String firstletter = String.valueOf(name1.charAt(0));
System.out.println(“Name is ” + firstletter +” “+ namearr[1]);
}
}
how to compre two strings with using any predefined methids?
Hi, good example. I have a doubt
Suppose I have to compare String s = “101011” and String s1 = “011010” element wise.
I can do this using character as shown in example. But, it would be easier if I’m able to make integer array .How to convert string to integer array.
write the code get the student name and three of hes subject mar; input using input box as the following
name
comp mark
eng mark
sin mark
then find avg?
Bad title.
char[] it’s not Character[].