Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Convert a string to an array

Ads

 
 

Share on Google+Share on Google+

Fathi
Convert a string to an array
1 Answer(s)      7 years ago
Posted in : JSP-Interview Questions

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

Ads
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


Ads









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 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 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 parse string into array
php parse string into array  How can i parse a string into array and convert an array into string
array to string
array to string  hello how to assign value from array to string. nsstring *abc = [array objectAtindex:1];   you can use this code NSString *abc = [NSString stringWithString:[array objectAtIndex:i]]; where 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<
convert string to date nsdate
convert string to date nsdate  Hi, How to convert c string to date NSDate?   Convert NSDate to NSString
PHP Array to String Conversion
PHP Array to String Conversion Sometimes data arrive in array which has to be convert into string, in this situation we can use implode() function which is used to convert the array into string. In PHP, conversion of an array to string
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
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
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 string
array string  how to sort strings with out using any functions
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
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 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
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
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
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
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 string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
convert zip file to byte array
convert zip file to byte array  Hi,How to convert zip file to byte array,can you please provide program?? Thanks, Ramanuja
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
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
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