我需要将字符串转换为字符串数组。例如:将字符串转换为java中的数组字符串
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";
请指导我
我需要将字符串转换为字符串数组。例如:将字符串转换为java中的数组字符串
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]);
}
,如果你需要删除的空间,你可以调用通过循环.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基础知识。我猜你可能需要初始化。 'String [] words1 = new String [5]'就是这样。不要使用像'Words1'和'words1'这样冲突的名字。变量也以小写字母开头。 – Nishant
用你的心思。而已 –