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}"));
6
  • 4
    Please give us sample input/output. No one wants to guess. Commented Sep 9, 2014 at 20:54
  • What do you think split("[a-zA-Z]{20}") does? Commented Sep 9, 2014 at 20:58
  • Should it be split("\.{20}")? Commented Sep 9, 2014 at 21:00
  • I am not saying you should change it, I am asking what do you think it does (what results do you think you should get)? Also which part it its documentation makes you think that such results should be expected? Commented Sep 9, 2014 at 21:00
  • @Pshemo, it splits string if it finds consecutive 20 characters in set [a-zA-Z]. White spaces won't match. Right? Commented Sep 9, 2014 at 21:02

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

6 Comments

Nice and succinct! I am not really understand this part ?<=\\G.
+1 Nice split regex. There are not many who know this needs \G, or even what \G does.
@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 this reminds me of your stellar answer, which was how I learned about \G. It is still my favourite answer on the site - it changed my life :)
In other words it means , match an empty string from the end of previous match after 15 characters.
|
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.