Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to convert string to char array in java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 05-02-2010
Member
 
Join Date: Aug 2009
Posts: 57
How to convert string to char array in java?
  

Hello to all,
I am new to this forum. I am working on one live project where I use java as front end and SQL as back end. In my project I have to convert string to char array. I tried various method but none of them worked out. Can anyone tell me how to convert string to char array in java? Please help me.
Thank you.

Reply With Quote
  #2  
Old 05-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: How to convert string to char array in java?

I have written following program to convert string to char array in java. It is very simple program. In the following program I have created one class known as StringToArrayEg. It is very simple program. I have create one variable to store string known as "literal".


Code:
package org.kodejava.example.lang;

public class StringToArrayEg
{
    public static void main(String[] args)
    {
        
        String literals = " Techarena -- the complete tech. solution  ;
        
        
        char[] temp = literal.toCharArray();
        
       
        for (int k = 0; k < temps.length; k++)
        {
            System.out.print(temps[k]);
        }
        
    }
}
Reply With Quote
  #3  
Old 05-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: How to convert string to char array in java?

It is very simple program to convert string to char array in java. You have to just use valueOf( ) method to convert string to char array. In the following program I have create one class known as strtochar class. After that I have create one variable of array known as "chs" and I have place some value on it. After that I have pass this variable to valueOf( ) method.


Code:
public class strtochar
  public static void main(String[] arg) {
    char[] chs = {'x','y','z','l'};
    System.out.println(String.valueOf(chs));   

  }
}
Reply With Quote
  #4  
Old 05-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
Re: How to convert string to char array in java?

You have to use String class for representing character strings. When you use any string literals in java program, that literals are used as instances of string class. Strings are constant value. It's value can not be changed once they are created. We can declare it in following way.

String opt = "roman";


is equivalent to:

char sam[] = {'r', 'o', 'm' , 'a' , 'n'};
String opt = new String(sam);
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 05-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: How to convert string to char array in java?

I have written following program for you to convert string to char array. It is very simple program. In the following program I have create one class known as stringtochar class. After that I have create one variable to store string. after that I have pass this variable to toCharArray() method for converting string to char array.


Code:
public class stringtochar
{
    public static void main(String args[])
    {
        String sos = "Hello to Techarena world";
        char array[] = sos.toCharArray();
		
        for (int k=0; ki<array.length; k++)
            System.out.print(array[k]);
        System.out.println();
    }
}
Reply With Quote
  #6  
Old 05-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: How to convert string to char array in java?

You have to just use toCharArray() to convert string to char array in java. It is very simple program. In the following program I have create one class known as SToCArrayEg class. After that I have create string variable like strOrigs to store string. After that I have pass this variable to toCharArray() method for converting string to char array.

Code:
public class SToCArrayEg {

 

public static void main(String args[]){

String strOrigs = "welcome to this world";



char[] stringArrays;


stringArrays = strOrig.toCharArray();

 



for(int indes=0; indes < stringArrays.length; indes++)

System.out.print(stringArrays[indes]);

 

}

 

}
Reply With Quote
  #7  
Old 10-01-2011
Member
 
Join Date: Jan 2011
Posts: 1
Re: How to convert string to char array in java?

Thanks, I find this code helpful . It helped me write this code for Java. This program takes users input in string and convert to char array and reads the output from the char array back and outputs with spacings between the string.

Code:
import java.io.*;

public class StringToCharArray
{
public static void main (String [] args) throws IOException
	{
		String myString;
		char myArray[];
		BufferedReader dataIn= new BufferedReader(new
		InputStreamReader(System.in));
		System.out.print("Enter a line of numbers or letters: ");
		myString = dataIn.readLine();			
		myArray = myString.toCharArray();
 		
                for (int i = 0; i < myArray.length; i++)
	        {
		System.out.print(myArray[i] + "     ");
		}
	}
}
Maybe someone will find this useful one day. Until then I will keep practicing to be the best programmer I can be.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to convert string to char array in java?"
Thread Thread Starter Forum Replies Last Post
How to convert array to collection in java? Steadfast Software Development 4 06-02-2010 04:22 PM
How to convert InputStream to String in java? KALIDA Software Development 4 20-01-2010 06:16 PM
JavaScript to convert an Array to String KAMANA Software Development 3 09-12-2009 05:30 AM
How to convert string into int in Java Zool Software Development 3 09-11-2009 12:41 PM
NullPointerException when using array of string in Java Sean J Software Development 2 25-04-2009 08:14 PM


All times are GMT +5.5. The time now is 06:48 PM.