Java StringTokenizer,空null标记 [英] Java StringTokenizer, empty null tokens

查看:213
本文介绍了Java StringTokenizer,空null标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字符串拆分为29个令牌..... stringtokenizer不会返回null令牌。我试过string.split,但我相信我做错了什么:

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);

样本输入:

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,,,,


推荐答案

如果你想保留尾随的空字符串,但是你没有想要给出最大的幻数,使用负限制:

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:

line.split(",", -1)

如果 line.equals(a,c),然后 line.split(,, - 1)[1] .isEmpty();它不是 null 。这是因为当是分隔符时,,,在两个分隔符之间有一个空字符串,而不是 null



示例:


使用上面的解释,请考虑以下示例: ,,

If line.equals("a,,c"), then line.split(",", -1)[1].isEmpty(); it's not null. This is because when "," is the delimiter, then ",," has an empty string between the two delimiters, not null.


Example:

Using the explanation above, consider the following example: ",,"

虽然您可能期望 null

实际结果是



如果你想要 null 而不是<$返回的数组中的空字符串c $ c> split ,然后你必须手动扫描数组并用 null 替换它们。我不知道为什么 s == null 优于 s.isEmpty()


If you want null instead of empty strings in the array returned by split, then you'd have to manually scan the array and replace them with null. I'm not sure why s == null is better than s.isEmpty(), though.

  • Java String.indexOf and empty strings

这篇关于Java StringTokenizer,空null标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆