1

I have a following code to split my input String:

import org.apache.commons.lang.StringUtils;

public class SplitTest {

    public static void main(String args[]) {
        String input = "A||B||||D||E";
        String result[] = StringUtils.split(input, "||");

        for (int i = 0; i < result.length; i++) {
            System.out.println("result[" + i + "]:" + result[i]);
        }
    }
}

And I was expecting output as:

Expected Output:

 result[0]:A
 result[1]:B
 result[2]:
 result[3]:D
 result[4]:E

Real Output:

result[0]:A
result[1]:B
result[2]:D
result[3]:E

Can anyone tell me why this is happening? What Can I do to achieve my expected output?

Chandra Prakash
  • 781
  • 4
  • 13
  • 23
  • `input.split()` from the [standard Java API](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)) will give you want you need, I believe. – Dan Temple Jun 11 '14 at 06:45

3 Answers3

2

The documentation says, that adjacent separators are treated as one separator

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#split(java.lang.String, java.lang.String)

Try using StrTokenizer instead

String input = "A||B||||D||E";
StrTokenizer tokenizer = new StrTokenizer(input, "||");
tokenizer.setIgnoreEmptyTokens(false);

for (int i = 0; i < tokenizer .getTokenArray().length; i++) {
    System.out.println("result[" + i + "]:" + tokenizer.getTokenArray()[i]);
}
Acheron
  • 43
  • 7
2

The method you are looking for is StringUtils.splitByWholeSeparatorPreserveAllTokens():

String result[] = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, "||");

You could also achieve the same result with standard Java and without library with String.split() and a regular expression:

String result[] = input.split("\\|\\|");
user432
  • 3,506
  • 17
  • 24
  • 1
    It's worth mentioning that String#split will have to compile the regex every time it's called. I don't understand why they didn't overload that function with a `Pattern` argument. – RTF Mar 15 '17 at 10:34
1

This is the expected behavior of StringUtils.split. This method will not return empty results.

See more: StringUtils::split(String, String) documentation

My solution will be:

public class SplitTest {

  public static void main(String args[]) {
    String input = "A||B||||D||E";
    String result[] = input.split("[|]{2}");

    for (int i = 0; i < result.length; i++) {
      System.out.println("result[" + i + "]:" + result[i]);
    }
  }
}

Or:

StringUtils::splitByWholeSeparatorPreserveAllTokens(String, String) documentation

Ambrish
  • 3,627
  • 2
  • 27
  • 42