简体   繁体   English

字符串拆分不返回空结果

[英]String split not returning empty results

I'm trying to use 我正在尝试使用

"value1:value2::value3".split(":");

Problem is that I want it to include the blank results. 问题是我希望它包含空白结果。

It returns: [value1, value2, value3] 它返回: [value1, value2, value3]
It should be: [value1, value2, , value3] 它应该是: [value1, value2, , value3]

Does anyone know the regexp to fix this? 有谁知道regexp来解决这个问题?

Ok I found cause of problem. 好的,我找到了问题原因。 I'm actually reading a text file and it contains this line: 我实际上正在阅读一个文本文件,它包含以下行:

123:;~\&:ST02:M:test:M:4540145::type;12:51253:D:2.2:567766::AL:::::::2.2b

When I process this line reading the text file it produces the erroneous result mentioned above, which is it doesn't include any empty results in cases like this: :::::. 当我处理这一行读取文本文件时,它会产生上面提到的错误结果,在这种情况下它不包含任何空结果::::::。

But when I use the above line in a test program it doesn't compile and I get a "invalid escape sequence". 但是当我在测试程序中使用上面的行时,它不会编译,我得到一个“无效的转义序列”。 I think its because of the "\\&". 我认为这是因为“\\&”。

Is there a workaround to this problem by using a regular expression? 使用正则表达式是否有解决此问题的方法?

split does include empty matches in the result, have a look at the docs here . split在结果中包含空匹配,请查看此处文档 However, by default, trailing empty strings (those at the end of the array) are discarded. 但是,默认情况下,将丢弃尾随空字符串(数组末尾的字符串)。 If you want to include these as well, try split(":", -1) . 如果您还想包含这些,请尝试split(":", -1)

Works for me. 适合我。

class t {
    public static void main(String[] _) {
        String t1 = "value1:value2::value3";
        String[] t2 = t1.split(":");
        System.out.println("t2 has "+t2.length+" elements");
        for (String tt : t2) System.out.println("\""+tt+"\"");
    }
}

gives the output 给出输出

$ java t
t2 has 4 elements
"value1"
"value2"
""
"value3"

我认为一个StringTokenizer可能对你更好,YMMV。

I don't honestly see the big draw of split. 我真的没有看到分裂的大抽奖。 StringTokenizer works just as well for most things like this and will easily send back the tokens (so you can tell there was nothing in between :: ). StringTokenizer对于像这样的大多数事情都可以正常工作,并且可以轻松地发送回令牌(所以你可以告诉它们之间没有任何内容::)。

I just wish it worked a little better with the enhanced for loop, but that aside, it wouldn't hurt to give it a try. 我只是希望它对增强的for循环有所改善,但除此之外,试一试也不会有什么坏处。

I think there is a regexp trick to get your matched tokens to return as well but I've gone 20 years without learning regexp and it's still never been the best answer to any problem I've tackled (Not that I would actually know since I don't ever use it, but the non-regexp solutions are generally too easy to beat.) 我认为有一个正则表达式的技巧可以让你的匹配代币返回,但是我已经走了20年没有学习正则表达式而且它仍然不是我解决过的任何问题的最佳答案(不是因为我真的知道了不要使用它,但非正则表达式解决方案通常太容易被击败。)

Use a negative limit in your split statement: 在split语句中使用负限制:

String str = "val1:val2::val3";
String[] st = str.split(":", -1);
for (int i = 0; i< st.length; i++)
    System.out.println(st[i]);

Results: 结果:

val1
val2

val3
public static void main(String[] args){
  String[] arr = "value1:value2::value3".split(":");
  for(String elm:arr){
    System.out.println("'"+elm+"',");
  }
  System.out.println(arr.length);
}

prints 版画

'value1',
'value2',
'',
'value3',
4

Which is exactly what you want. 这正是你想要的。 Your mistake is somewhere else... 你的错误在别的地方......

Using Guava 's Splitter class: 使用GuavaSplitter类:

Iterable<String> split = Splitter.on(':').split("value1:value2::value3");

Splitter does not omit empty results by default, though you can make one that does. Splitter默认情况下不会省略空结果,但您可以创建一个。 Though it seems from what others are saying that what you're doing should work as well. 虽然看起来其他人说的是你正在做的事也应该有效。

This works, 这有效,

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;

public class split {
public static void main(String[] args)
{
    String data = null;
    try {
    BufferedReader br = new BufferedReader(new FileReader(new File("split.csv")));
    while( (data=br.readLine())!=null)
    {
        System.out.println("line:"+data);
        String[] cols = data.split(":",-1);
        System.out.println("count:"+cols.length);
        for(int x=0;x<cols.length;++x)
        {
            System.out.println("["+x+"] =("+cols[x]+")");
        }
    }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Here is a test file, 这是一个测试文件,

a:b:c:d:e
a:b:c:d:
a:b:c::
a:b::: 
a::::
::::
::::e
:::d:e
::c:d:e
:b:c:d:e
a:b:c:d:e

如果你还有问题,那应该可以使用StringTokenizer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM