【发布时间】:2012-08-25 09:30:29
【问题描述】:
我有一个由空格分隔的几个单词的字符串,例如“firstword second third”和一个 ArrayList。我想将字符串分成几部分,并将“部分”字符串添加到 ArrayList。
例如,“firstword second third”可以拆分为三个单独的字符串,因此 ArrayList 将有 3 个元素; "1 2 3 4" 可以拆分为 4 个字符串,在 ArrayList 的 4 个元素中。请看下面的代码:
public void separateAndAdd(String notseparated) {
for(int i=0;i<canBeSepartedinto(notseparated);i++{
//what should i put here in order to split the string via spaces?
thearray.add(separatedstring);
}
}
public int canBeSeparatedinto(String string)
//what do i put here to find out the amount of spaces inside the string?
return ....
}
如果您不明白我的意思,请发表评论,或者我应该修复这篇文章中的一些错误。谢谢你的时间!
【问题讨论】:
标签: java arrays string arraylist split