Java StringTokenizer,空空标记
我正在尝试将一个字符串拆分为 29 个标记...... stringtokenizer 不会返回空标记。我尝试过 string.split,但我相信我做错了什么:
String [] strings = line.split(",", 29);
示例输入:
10150,15:58,23:58,16:00,00:00,15:55,23:55,15:58,00:01,16:03,23:58,,,,,16:00,23:22,15:54,00:03,15:59,23:56,16:05,23:59,15:55,00:01,,,,
10155,,,,,,,,,,,07:30,13:27,07:25,13:45,,,,,,,,,,,07:13,14:37,08:01,15:23
10160,10:00,16:02,09:55,16:03,10:06,15:58,09:48,16:07,09:55,16:00,,,,,09:49,15:38,10:02,16:04,10:00,16:00,09:58,16:01,09:57,15:58,,,,
I am trying to split a string into 29 tokens..... stringtokenizer won't return null tokens. I tried string.split, but I believe I am doing something wrong:
String [] strings = line.split(",", 29);
sample inputs:
10150,15:58,23:58,16:00,00:00,15:55,23:55,15:58,00:01,16:03,23:58,,,,,16:00,23:22,15:54,00:03,15:59,23:56,16:05,23:59,15:55,00:01,,,,
10155,,,,,,,,,,,07:30,13:27,07:25,13:45,,,,,,,,,,,07:13,14:37,08:01,15:23
10160,10:00,16:02,09:55,16:03,10:06,15:58,09:48,16:07,09:55,16:00,,,,,09:49,15:38,10:02,16:04,10:00,16:00,09:58,16:01,09:57,15:58,,,,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望保留尾随的空字符串,但又不想给出最大值的幻数,请使用负数限制:
If
line.equals("a,,c")
,然后line.split(",", -1)[1].isEmpty();它不是null
。这是因为当","
为分隔符时,",,"
两个分隔符之间有一个空字符串,而不是null
。Example:
使用上面的解释,考虑以下示例:
",,"
尽管您可能期望
","
、null
和 <代码>“,”。实际结果是
","
,""
和","
If you want
null
instead of empty strings in the array returned bysplit
, then you'd have to manually scan the array and replace them withnull
. I'm not sure whys == null
is better thans.isEmpty()
, though.另请参阅
String.indexOf
和空字符串If you want the trailing empty strings to be kept, but you don't want to give a magic number for maximum, use a negative limit:
If
line.equals("a,,c")
, thenline.split(",", -1)[1].isEmpty()
; it's notnull
. This is because when","
is the delimiter, then",,"
has an empty string between the two delimiters, notnull
.Example:
Using the explanation above, consider the following example:
",,"
Although you might expect
","
,null
, and","
.The actual result is
","
,""
and","
If you want
null
instead of empty strings in the array returned bysplit
, then you'd have to manually scan the array and replace them withnull
. I'm not sure whys == null
is better thans.isEmpty()
, though.See also
String.indexOf
and empty strings在 Apache Commons Lang 库中使用
StringUtils.splitPreserveAllTokens()
Use
StringUtils.splitPreserveAllTokens()
in Apache Commons Lang library如果您希望保留空令牌,
string.split
将无法令人满意地工作。StringTokenizer
也不起作用。我提供了以下方法,可能对你有帮助
If you want empty tokens to be retained
string.split
won't work satisfactorily.StringTokenizer
will also no work.I have come with following method, which might be helpful for you
此类提供了一些简单的功能,提供了易于使用的方法来在分隔字符串(例如 CSV 字符串)与集合和数组之间进行转换。
This class delivers some simple functionality provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.
如果您希望保留空令牌,
string.split()
将无法令人满意地工作。StringTokenizer
也将不起作用。我提供了以下方法,可能对你有帮助:If you want empty tokens to be retained
string.split()
won't work satisfactorily.StringTokenizer
will also not work. I have come with following method, which might be helpful for you: