Results 1 to 7 of 7

Thread: How to convert string to char array in java?

  1. #1
    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.

  2. #2
    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]);
            }
            
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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));   
    
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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);

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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();
        }
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    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]);
    
     
    
    }
    
     
    
    }

  7. #7
    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.

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,303,160.12869 seconds with 16 queries