在Java中使用多个连续逗号分割字符串[重复]

时间:2022-11-28 21:39:05

This question already has an answer here:

这个问题在这里已有答案:

String abc = "a,b,c,d,,,";
String[] arr = abc.split(",");
System.out.println(arr.length);

Output is 4. But obviously my expectation is 7. Here is my solution:

输出是4.但显然我的期望是7.这是我的解决方案:

String abc = "a,b,c,d,,,";
abc += "\n";
String[] arr = abc.split(",");
System.out.println(arr.length);

Why does it happen? Anyone could give my a better solution?

为什么会这样?谁能提供更好的解决方案?

5 个解决方案

#1


8  

Use the alternative version of String#split() that takes two arguments to achieve this:

使用String#split()的替代版本,它使用两个参数来实现此目的:

String abc = "a,b,c,d,,,";
String[] arr = abc.split(",", -1);
System.out.println(arr.length);

This prints

这打印

7

From the Javadoc linked above:

从上面链接的Javadoc:

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

如果n是非正数,那么模式将被应用尽可能多的次数,并且数组可以具有任何长度。如果n为零,那么模式将被应用尽可能多的次数,该数组可以具有任何长度,并且将丢弃尾随的空字符串。

#2


4  

You can use lookahead:

你可以使用lookahead:

String abc = "a,b,c,d,,,";
String[] arr = abc.split("(?=,)");
System.out.println(arr.length); //7

#3


1  

Use:

使用:

String[] arr = abc.split("(?=,)");

to split abc

拆分abc

#4


1  

This is because split does not include only trailing empty strings, but if you have \n at the end, then the last element is not empty

这是因为split不仅包含尾随空字符串,但如果你在末尾有\ n,那么最后一个元素不为空

[a, b, c, d, , , \n]

#5


0  

split() is coded to that very job. You can count a character or string occurrence in the below way.

split()被编码为那个工作。您可以通过以下方式计算字符或字符串的出现次数。

        String ch = ",";
    int count = 0;
    int pos = 0;
    int idx;
    while ((idx = abc.indexOf(ch, pos)) != -1) {
        ++count;
        pos = idx + sub.length();
    }
    System.out.println(count);

This very logic is used in Spring.

这个逻辑在Spring中使用。

#1


8  

Use the alternative version of String#split() that takes two arguments to achieve this:

使用String#split()的替代版本,它使用两个参数来实现此目的:

String abc = "a,b,c,d,,,";
String[] arr = abc.split(",", -1);
System.out.println(arr.length);

This prints

这打印

7

From the Javadoc linked above:

从上面链接的Javadoc:

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

如果n是非正数,那么模式将被应用尽可能多的次数,并且数组可以具有任何长度。如果n为零,那么模式将被应用尽可能多的次数,该数组可以具有任何长度,并且将丢弃尾随的空字符串。

#2


4  

You can use lookahead:

你可以使用lookahead:

String abc = "a,b,c,d,,,";
String[] arr = abc.split("(?=,)");
System.out.println(arr.length); //7

#3


1  

Use:

使用:

String[] arr = abc.split("(?=,)");

to split abc

拆分abc

#4


1  

This is because split does not include only trailing empty strings, but if you have \n at the end, then the last element is not empty

这是因为split不仅包含尾随空字符串,但如果你在末尾有\ n,那么最后一个元素不为空

[a, b, c, d, , , \n]

#5


0  

split() is coded to that very job. You can count a character or string occurrence in the below way.

split()被编码为那个工作。您可以通过以下方式计算字符或字符串的出现次数。

        String ch = ",";
    int count = 0;
    int pos = 0;
    int idx;
    while ((idx = abc.indexOf(ch, pos)) != -1) {
        ++count;
        pos = idx + sub.length();
    }
    System.out.println(count);

This very logic is used in Spring.

这个逻辑在Spring中使用。