在java中将字符串转换为数组字符串

在java中将字符串转换为数组字符串,java,arrays,string,Java,Arrays,String,我需要将字符串转换为字符串数组。例如: String words = "one, two, three, four, five"; 成阵列状 String words1[]; String words1[0]="one"; words1[1]="two"; words1[2]="three"; words1[3]="four"; words1[4]="five"; 请引导我我想你要找的也许是: String words = "one

我需要将字符串转换为字符串数组。例如:

String words = "one, two, three, four, five";
成阵列状

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

请引导我

我想你要找的也许是:

String words = "one two three four five";
String[] words1 = words.split(" ");

准确答案将使用
split()
函数,正如大家建议的那样:

String words = "one, two, three, four, five";
String words1[] = words.split(", ");
试试这个

 String word = " one, two, three, four, five";        
 String words[] = word.split(",");
 for (int i = 0; i < words.length; i++) {
     System.out.println(words[i]);
 }
String word=“一、二、三、四、五”;
字符串字[]=word.split(“,”);
for(int i=0;i

如果需要删除空间,可以调用
.trim()方法。

这里我编写了一些代码,可能对您有所帮助,请看一看

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

阅读Java基础知识。我想你可能需要初始化<代码>字符串[]单词1=新字符串[5]
仅此而已。不要使用冲突名称,如
Words1
Words1
。变量也是以小写字母开头的。动动脑筋。就这样