Tokenising a String containing empty tokens

家住魔仙堡 提交于 2019-11-30 13:55:41

Pass a -1 to split as the limit argument:

String s = ",abd,def,,ghi,";
String[] tokens = s.split(",", -1);

Then your result array will include any trailing empty strings.

From the javadocs:

If [the limit] is non-positive then the pattern will be applied as many times as possible and the array can have any length. If [the limit] is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Calling split(regex) acts as if the limit argument is 0, so trailing empty strings are discarded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!