i need to convert String to array of Strings. eg:

String words = "one, two, three, four, five";

into array like

String words1[];
String words1[0]="one";
       words1[1]="two";
       words1[2]="three";
       words1[3]="four";
       words1[4]="five";  

please guide me

share|improve this question

0% accept rate
5  
read Java basics. I guess all you need to initialize perhaps. String[] words1 = new String[5] that's all. don't use conflicting name like Words1, and words1. Also variables start with small letter. – Nishant Sep 7 '12 at 5:22
Use your mind. that's it – Jayant Varshney Sep 7 '12 at 12:27
1  
improve your accept rate – sfah Nov 26 '12 at 6:30
feedback

4 Answers

I think perhaps what you are looking for is:

String words = "one two three four five";
String[] words1 = words.split(" ");
share|improve this answer
feedback

The exact answer will be using split() function as everyone suggested:

String words = "one, two, three, four, five";
String words1[] = words.split(", ");
share|improve this answer
feedback

Try this,

 String word = " one, two, three, four, five";        
 String words[] = word.split(",");
 for (int i = 0; i < words.length; i++) {
     System.out.println(words[i]);
 }

if you need to remove space, you can call .trim(); method through the loop.

share|improve this answer
feedback

Here i code something may it helpful to you just take a look.

import java.util.StringTokenizer;

public class StringTokenizing
{
 public static void main(String s[])
 {
   String Input="hi hello how are you";
 int i=0;
   StringTokenizer Token=new StringTokenizer(Input," ");
   String MyArray[]=new String[Token.countTokens()];
   while(Token.hasMoreElements())
   {
    MyArray[i]=Token.nextToken();
    System.out.println(MyArray[i]);
    i++;
   }
  }
 }
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.