我需要将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";
请指导我
答案 0 :(得分:2)
我想也许您正在寻找的是:
String words = "one two three four five";
String[] words1 = words.split(" ");
答案 1 :(得分:2)
确切答案将按照所有人的建议使用split()
功能:
String words = "one, two, three, four, five";
String words1[] = words.split(", ");
答案 2 :(得分:0)
试试这个,
String word = " one, two, three, four, five";
String words[] = word.split(",");
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
如果您需要删除空格,可以通过循环调用.trim();
方法。
答案 3 :(得分:0)
我在这里编写代码可能对您有所帮助。
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++;
}
}
}