Convert a string to an array

Convert a string to an array

Hello,

I have an String variable and I want to convert it to array and then print on the console or use it in our program.

I want to sprint the string into array wherever space is found.

Thanks

View Answers

October 23, 2010 at 10:42 PM

Hi,

You can easily convert a String to an array in Java with the help of Split function. The following code example shows how you can convert string to array and then print it on the console.

import java.util.*;

public class ConvertStringToArray {

    public static void main(String[] args) {

        String stringMsg="How to convert String to Array in Java";


        String[] arr=stringMsg.split(" ");
        System.out.println("Length of Array is :"+arr.length);
        for(int i=0;i<arr.length;i++)
        {
            System.out.println("array "+i+":"+arr[i]);
        }
    }
}

Output of the program is:

C:\>javac ConvertStringToArray.java

C:\>java ConvertStringToArray
Length of Array is :8
array 0:How
array 1:to
array 2:convert
array 3:String
array 4:to
array 5:Array
array 6:in
array 7:Java

Thanks









Related Tutorials/Questions & Answers:
Convert Array to String
Convert Array to String       In this program we have taken an array of type String as "String stringarray[] = {"chandan", " " ,"tamana"
Convert string into character array
Description: This tutorial demonstrate how to convert a string into array of char. The toCharArray() method converts string to a new character array...;{   public static void main(String[] args) {  
Advertisements
how to convert string to char array in Java
how to convert string to char array in Java  Hi, I have a string in Java which has to be converted to char array. What are the options to convert String object to char Array in Java? how to convert string to char array
How to convert Arraylist into String Array Java
How to convert Arraylist into String Array Java  Hi, I am beginners of Java programming. Can somebody Suggest me how to convert arraylist to string... into string array using java language. Please visit the below link: http
Convert a string representation of a hex dump to a byte array using Java?
Convert a string representation of a hex dump to a byte array using Java?  Convert a string representation of a hex dump to a byte array using Java
how to convert string to char array in java without using tochararray
how to convert string to char array in java without using tochararray  Hi, I want to convert the String object in java without the toChararray() function. how to convert string to char array in java without using
array to string
array to string  hello how to assign value from array to string. nsstring *abc = [array objectAtindex:1];   you can use this codeADS_TO_REPLACE_1 NSString *abc = [NSString stringWithString:[array objectAtIndex:i
string array
string array   Hi hw to print string array element in ascending r... StringArray { public static void main(String[] args) { String arr...); System.out.println("Array Elements in Ascending Order: "); for(int i=0;i<
array of string
array of string  Waht is the problem in this code? import java.util.Scanner; public class LastCheck { public static void main(String args[]){ int a; Scanner s= new Scanner(System.in); a=s.nextInt(); String ar[]=new
php parse string into array
php parse string into array  How can i parse a string into array and convert an array into string
array string
array string  how to sort strings with out using any functions
How to Convert ArrayList to Array?
How to Convert ArrayList to Array?  Hi, I am trying to learn to Convert ArrayList to Array. How it is possible? How to Convert ArrayList to Array? Thanks   Hi, To Convert ArrayList to array ArrayList.toArray
PHP Array to String Conversion
is used to convert the array into string. In PHP, conversion of an array to string...: Convert Array to String in PHP Example 2:ADS_TO_REPLACE_7 <?php...PHP Array to String Conversion Sometimes data arrive in array which has
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
string array sort
string array sort  Hi. How to sort a string array
Java arraylist to string array
arrayList can be converted into string array. For this first converts the arrayList into the object array. This is done by  using the toArray() method. Then cast it into the string and assign in the String array
Convert array to Vector
Convert array to Vector       In this section we will learn how to convert an array to vector. Actually array is used is used to store similar data types but Vector is used
String Array - Java Beginners
String Array  From where can I get, all the functions that are needed for me to manipulate a String Array. For Example, I had a String Array ("3d4..., as to by which method can I separate the Integers from this Array of String
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
Array /string based problem....
Array /string based problem....  thanx to help me.but again a problem...("Enter string: "); String[] array = new String[5]; for(int i=0;i<5;i...]+" "); } } static String[] selectionSort(String[] array) { for (int i = 1
string array based problem
string array based problem  R/sir, firstly thanks... string: "); String[] array = new String[5]; for(int i=0;i<5;i++){ array[i...++){ System.out.print(array[i]+" "); } } static String[] selectionSort(String[] array
convert string to date nsdate
convert string to date nsdate  Hi, How to convert c string to date NSDate?   Convert NSDate to NSString
String Array - Java Beginners
for me to manipulate a String Array. For Example, I had a String Array ("3d4..., as to by which method can I separate the Integers from this Array of String... this question to you before, now the problem comes if my String Array consisted
How to convert a String to an int?
How to convert a String to an int?  I my program I am taking... format. Now I want to convert into int value. Tell me How to convert a String... contains the value 1025, then you can use the following code to convert the String
How to convert String into java.util.Date
How to convert String into java.util.Date  Hi, How I can convert String date into java.util.Date in a Java program? Thanks   Hi... Getting time in Milliseconds Convert Date To Calendar How to convert String Date
JavaScript split string into array
; Description: The split() method is used to split a string into an array...JavaScript split string into array  How to split the string into array?   <html> <head> <title></title> <script
How to Convert String to Date?
How to Convert String to Date?  Hi, I am new in Java and learning... have to convert it to Date object. Tell me How to Convert String to Date? Thanks   Hi Dude, Its very easy to convert the String into date format
How to convert Collection to Array in Java?
How to convert Collection to Array in Java?  Hello, I have a collection object in data with data in it. How to convert Collection to Array in Java... to Array in Java. Here is the code for converting Collection to Array: String
Read/Convert an inputStream to a String
in Java and convert it to a String. what is the best code for for this? How to Read/Convert an inputStream to a String? My object is of java.io.InputStream...://www.roseindia.net"; //Convert String into InputStream InputStream
how to convert string to image in java
how to convert string to image in java  how to convert string to image in java? I know we need to convert image to a byte array at some point in my application but do not know how to convert a image into byte string. Please
array and string based problem
array and string based problem  this program is accepting only... main(String a[]){ Scanner input=new Scanner(System.in); int array[]=new..._srt(int array[],int n){ for (int j = 0; j < n; j++){ int i = j-1
array and string based problem
array and string based problem  this program is accepting only... main(String a[]){ Scanner input=new Scanner(System.in); int array[]=new..._srt(int array[],int n){ for (int j = 0; j < n; j++){ int i = j-1
array and string based problem
array and string based problem  this program is accepting only... main(String a[]){ Scanner input=new Scanner(System.in); int array[]=new..._srt(int array[],int n){ for (int j = 0; j < n; j++){ int i = j-1
Array List and string operation
Array List and string operation  hi, I want to search an arraylist element in a a given file. example I have a name called "mac" in my arraylist and I have a txt file which contains mac, how many times "mac" appears in the txt
array split string
array split string  array split string   class StringSplitExample { public static void main(String[] args) { String st = "Hello_World"; String str[] = st.split("_"); for (int i = 0; i <
Convert Vector to Array
Convert Vector to Array       In this section, you will learn to convert Vector to an Array. ... to an Array. Here we have taken a Vector v.add("Java, is, 
Convert List to Array
Convert List to Array       Lets see how to convert List to Array.  Code Description... a method Mylist.toArray to convert the List to Array. Here is the code
Convert Array to List
Convert Array to List       In this section, you will learn to convert an Array to List.  Code Description:ADS_TO_REPLACE_1 This program helps you in converting an Array
How ro convert char into string??
How ro convert char into string??  Suppose i have one array like... char [] ch={'H','E','L','L','o'}; now i want to convert this arrar into string... is for java...but i want this in c# How ro convert char into string using recursive
How to Convert array to list in Java
How to Convert array to list in Java  Hi, I have object of array in Java and I want to convert it to list. How to Convert array to list in Java... ArrayToList { public static void main(String[] args) { List arrList = new
array, index, string
array, index, string  how can i make dictionary using array...please help
Convert String to long
Convert String to long       In this section, we will learn how to convert String to long. The following program provides you the functionality to convert String to long. Code
Convert charArray to String
Convert charArray to String       Lets see how to convert charArray to String.  Code...;new String(array);   System.out.println(s);     
Convert Array to Vector
Convert Array to Vector       In this section, you will learn to convert an Array to Vector... an Array into a Vector. Here we have taken an array of data which gets
String array sort
String array sort  Hi here is my code. If i run this code I am... result_set=null; String route_number[]=new String[1000]; String route_number1[]=new String[1000]; route_number1[0]=" "; int counter=0,count=0
String array sort
String array sort  Hi here is my code. If i run this code I am... result_set=null; String route_number[]=new String[1000]; String route_number1[]=new String[1000]; route_number1[0]=" "; int counter=0,count=0
Convert Collection to Array
Convert Collection to Array       In this section, you will learn to convert Collection data to an Array.  Code Description:ADS_TO_REPLACE_1 This program helps you

Ads