Java StringTokenizer,空的空令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2708591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java StringTokenizer, empty null tokens
提问by user69514
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:
我试图将一个字符串拆分为 29 个标记..... stringtokenizer 不会返回空标记。我试过 string.split,但我相信我做错了什么:
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,,,,
回答by polygenelubricants
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)
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.
如果line.equals("a,,c"), 那么line.split(",", -1)[1].isEmpty(); 不是null。这是因为 when","是分隔符,然后",,"在两个分隔符之间有一个空字符串,而不是null.
Example:例子:
Using the explanation above, consider the following example: ",,"
使用上面的解释,请考虑以下示例: ",,"
Although you might expect ",", null, and ",".
尽管您可能期望",", null, 和","。
The actual result is ",", ""and ","
实际结果是",", ""并且","
如果您想
nullnull在由 返回的数组中代替空字符串splitsplit,则必须手动扫描数组并将它们替换为nullnull。不过,我不确定为什么s == nulls == null比 更好s.isEmpty()s.isEmpty()。See also
也可以看看
回答by Tom
Use StringUtils.splitPreserveAllTokens()in Apache Commons Langlibrary
使用StringUtils.splitPreserveAllTokens()中的Apache Commons Lang中库
回答by Wasim
If you want empty tokensto be retained string.splitwon't work satisfactorily. StringTokenizerwill also no work.
I have come with following method, which might be helpful for you
如果您希望保留空令牌string.split将无法令人满意地工作。StringTokenizer也不会工作。我提供了以下方法,可能对您有所帮助
public static String[] splitTotokens(String line, String delim){
String s = line;
int i = 0;
while (s.contains(delim)) {
s = s.substring(s.indexOf(delim) + delim.length());
i++;
}
String token = null;
String remainder = null;
String[] tokens = new String[i];
for (int j = 0; j < i; j++) {
token = line.substring(0, line.indexOf(delim));
//System.out.print("#" + token + "#");
tokens[j] = token;
remainder = line.substring(line.indexOf(delim) + delim.length());
//System.out.println("#" + remainder + "#");
line = remainder;
}
return tokens;`
}
回答by Wasim
If you want empty tokens to be retained string.split()won't work satisfactorily. StringTokenizerwill also not work. I have come with following method, which might be helpful for you:
如果您希望保留空令牌string.split()将无法令人满意地工作。StringTokenizer也不会工作。我提供了以下方法,可能对您有所帮助:
public static String[] splitTotokens(String line, String delim){
String s = line;
int i = 0;
while (s.contains(delim)) {
s = s.substring(s.indexOf(delim) + delim.length());
i++;
}
String token = null;
String remainder = null;
String[] tokens = new String[i];
for (int j = 0; j < i; j++) {
token = line.substring(0, line.indexOf(delim));
// System.out.print("#" + token + "#");
tokens[j] = token;
remainder = line.substring(line.indexOf(delim) + delim.length());
//System.out.println("#" + remainder + "#");
line = remainder;
}
return tokens;
}
回答by user2675617
use this org.springframework.util.StringUtils
org.springframework.util.StringUtils.delimitedListToStringArray(data, delimit);
This class delivers some simple functionality provides easy-to-use methods to convert between delimited strings, such as CSV strings, and collections and arrays.
此类提供了一些简单的功能,提供了易于使用的方法来在分隔字符串(例如 CSV 字符串)以及集合和数组之间进行转换。

