1

I have below code

String str= new String("aaa,,bbb,,ccc,");
String[] strArray= str.split(",");

From this I am getting below values in strArray

strArray = [aaa,'',bbb,'',ccc];

But I want below output

strArray = [aaa,null,bbb,null,ccc,null];

I have written my own function to do this, but I want to know is there any standard method which can give this output?

If yes then please suggest

If no then please suggest if any improvement for below code.

public List<String> splitStr(String str, char character) {
    List<String> list = null;
    if (StringUtil.isNotEmpty(str)) {
        list = new ArrayList<String>();
        int i;
        char ch;
        int characterIndex, loopStartingIndex = 0;

        if (str.charAt(0) == character) {
            list.add(null);
        } else {
            list.add(str.substring(0, str.indexOf(character)));
            loopStartingIndex = str.indexOf(character);
        }

        int length = str.length();
        for (i = loopStartingIndex; i < length; i++) {
            ch = str.charAt(i);
            if (ch == character) {
                characterIndex = str.indexOf(character, i+1);
                if (characterIndex == i+1 || characterIndex == -1) {
                    list.add(null);
                } else {
                    list.add(str.substring(i+1, characterIndex));
                    i = characterIndex-1;
                }
            }
        }
    }
    /*for (String s : list) {
        System.out.println(s);
    }*/

    return list;
}

Edit:

I had tested the code for abc,,,, string with str.split(",") method and I got [abc] as output so I thought aaa,,bbb,,ccc will give me 3 values by split method. But it is giving [aaa,'',bbb,'',ccc] skipping ending commas.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • possible duplicate of [Java split() method strips empty strings at the end?](http://stackoverflow.com/questions/545957/java-split-method-strips-empty-strings-at-the-end) – Evan Knowles Nov 12 '14 at 07:38
  • Yes, my answer is present over that question. Sorry couldn't search it. – Naman Gala Nov 12 '14 at 07:45

4 Answers4

2

You can pass a negative value to String.split(String, int) like

String str = "aaa,,bbb,,ccc,";
String[] strArray = str.split(",", -1);
System.out.println(Arrays.toString(strArray));

Output is

[aaa, , bbb, , ccc, ]

This is helpfully documented in the linked javadoc as,

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length

Edit

public static void main(String[] args) {
    String str = "aaa,,bbb,,ccc,";
    String[] strArray = str.split(",", -1);
    for (int i = 0; i < strArray.length; i++) {
        if (strArray[i].isEmpty()) {
            strArray[i] = null;
        }
    }
    System.out.println(Arrays.toString(strArray));
}

Output is

[aaa, null, bbb, null, ccc, null]

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

how about something simpler

String str= new String("aaa,,bbb,,ccc,");
String[] strArray= str.split(",", -1);

                      This --     ^^
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

write a like :

         String str= new String("aaa,,bbb,,ccc,");
         String[] strArray= str.split(",");
         for(int i=0;i<strArray.length;i++)
            System.out.print(strArray[i]);

or

         System.out.print(Arrays.toString(strArray));

output:

   [aaa, , bbb, , ccc]
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

The following code

String str = "aaa,,bbb,,ccc";
String[] strArray = str.split(",");
System.out.println(Arrays.toString(strArray));

outputs [aaa, , bbb, , ccc], so the array has 5 elements, not 3 as you claim. The only thing you have to do is loop through the array and replace empty strings by null references to get what you want.

You can pass a negative number as the second argument of split, but this only effects empty strings at the end of the array. With a negative number, the empty string at the end are also included. That is,

"aaa,,bbb,".split(",")

returns ["aaa","","bbb"], while

"aaa,,bbb,".split(",",-1)

returns ["aaa","","bbb",""].

Hoopje
  • 12,677
  • 8
  • 34
  • 50
  • Yes, you are right. Actually I tested the code for `abc,,,,` string and I got abc as output so I thought `aaa,,bbb,,ccc` will give me 3 values by split method. – Naman Gala Nov 12 '14 at 08:04