0

There is string with fixed length and it has to be spitted by 15 characters each record. The results should be placed in List, however it seems like whole string always placed in 0 position in List.

Arrays.asList(a1.substring(1,324).split("[a-zA-Z]{20}"))

Why is that?

UPDATE:

List<String> l =  Arrays.asList("1111111111     1119999999                                                                                              ".split("[0-9]{15}"));
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185

2 Answers2

3

split regex should be :

String[] arr = str.split("(?<=\\G.{20})");

The above split str for every 20 chars

For example below code splits str for every 15 chars:

 String str ="hkdhadhkshdkhskhdkashdkasgi2oyeihsadkahdkashdlkhas";
             List<String> list = Arrays.asList(str.split("(?<=\\G.{15})"));
            System.out.println(list);

prints:

[hkdhadhkshdkhsk, hdkashdkasgi2oy, eihsadkahdkashd, lkhas]
user3487063
  • 3,672
  • 1
  • 17
  • 24
  • Nice and succinct! I am not really understand this part `?<=\\G`. – J.Olufsen Sep 09 '14 at 21:16
  • +1 Nice split regex. There are not many who know this needs `\G`, or even what `\G` does. – Bohemian Sep 09 '14 at 21:18
  • @RCola `\G` represents end of previous match, or if there is no previous match (in case where we are looking for first match) it represents start of the String (is equivalent of `^`). – Pshemo Sep 09 '14 at 21:24
  • Thanks @Phesmo for the explanation, in addition,as per my understanding `?<=\\G` means lookbehind at the end of previous match and `?<=\\G.{15}` means look behind at the end of previous match after 15 characters, the end of last match is where the split needs to be done. – user3487063 Sep 09 '14 at 22:45
  • 1
    @pshemo this reminds me of [your stellar answer](http://stackoverflow.com/a/16486373/256196), which was how I learned about `\G`. It is still my favourite answer on the site - it changed my life :) – Bohemian Sep 09 '14 at 23:10
  • 2
    In other words it means , match an empty string from the end of previous match after 15 characters. – user3487063 Sep 09 '14 at 23:28
0

From your question:

There is string with fixed length and it has to be spitted by 15 characters each record. The results should be placed in List.

String.split() isn't really suitable for something like this. It's designed to split on a delimiter, which "every 15 characters" is not.

You're looking for the functionality in String.substring(), which returns a String that is the sequence between [beginIndex, endIndex).

With this method:

public static List<String> splitByIndex(String toSplit, int index) { 
    List<String> result = new ArrayList<String>(); 
    for (int i = 0; i < toSplit.length(); i += index) { 
        if (i + index < toSplit.length()) {
            result.add(toSplit.substring(i, i + index));
        } else {
            result.add(toSplit.substring(i, toSplit.length() - 1));
        }
    }
    return result; 
}

You can split a String into a List<String> by a given number of characters. This example code:

String a1 = "I'm the fixed length string that needs to be split by 15 characters.";
List<String> list = splitByIndex(a1, 15);
System.out.println(list);

will output:

[I'm the fixed l, ength string th, at needs to be , split by 15 cha, racters]

jdphenix
  • 15,022
  • 3
  • 41
  • 74