Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Interview-Questions Convert a string to an array
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Fathi
Convert a string to an array
1 Answer(s)      5 years and a month 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


Advertisement
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 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
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
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 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 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
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
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
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
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
How to convert string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
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
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
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
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
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
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 charArray to String
Convert charArray to String       Lets see how to convert charArray to String.  Code...','a','m','e',')' };   String s = new String(array
Convert Vector to Array
Convert Vector to Array       In this section, you will learn to convert Vector to an Array.  Code Description: This program helps you in converting Vector to an Array. Here
Java convert file to byte array
Java convert file to byte array  How to convert file to byte array in Java? Thanks   Hi, To convert file to byte array in Java you have...) Use the insputStream.read(bytes); of the class to read the data into byte array
 

 

 

DMCA.com