我有一个由空格分隔的字符串,例如“firstword second third”,和一个ArrayList。我想把字符串分成几个片段,并将“piece”字符串添加到ArrayList中。
例如,“firstword second third”可以分为三个单独的字符串,因此ArrayList将有3个元素;“1 2 3 4”可以分为4个字符串,在ArrayList的4个元素中。请参见下面的代码:
例如,“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 ....
}
如果您不理解我的意思或者我在这篇文章中有错误的话,请留下评论。感谢您的时间!
Integer.parseInt(parts[i]);
?原帖并没有要求将分割后的字符串转换为整数。 - Eng.Fouad