gpt4 book ai didi

Java StringTokenizer,空的空标记

转载 作者:搜寻专家 更新时间:2023-10-30 21:43:28 24 4
gpt4 key购买 nike

我正在尝试将一个字符串拆分为 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,,,,

最佳答案

如果您希望保留尾随的空字符串,但又不想给出最大值的魔数(Magic Number),请使用负数限制:

line.split(",", -1)

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


示例:

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

尽管您可能期望 ","null","

实际结果是",", "" and ","


如果你想要 null 而不是 split 返回的数组中的空字符串,那么你必须手动扫描数组并将它们替换为 null。不过,我不确定为什么 s == nulls.isEmpty() 更好。

另见

关于Java StringTokenizer,空的空标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2708591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com