2

I have a String array

String student [] = {"BAT4M0ABBB4M0ABOH4M0CCHI4U0AENG4U0DMDM4U0B"}

I need to split this for every 7 characters. It should look like this:

String student {
"BAT4M0A",
"BBB4M0A",
"BOH4M0C",
"CHI4U0A",
"ENG4U0D",
"MDM4U0B"
}

This is my code so far.

       for (int i=0; i<= data.length; i++)
        {
          student= data[i].split(","); //data is a file of 1000 lines that is being read
          if (student [2].equals(sN)) //student is a line in the each file that has been  split into 11 parts
        {
            String timetable[]= student[9].split("(?<=\\G.......)");
            System.out.println (timetable);
            break;
        }
user3161311
  • 33
  • 1
  • 4

3 Answers3

6

You could do

String[] array = student[0].split("(?<=\\G.{7})");

\G is a meta character that matches the last match so (?<=\\G.{7}) matches an empty string followed by any 7 characters

Reimeus
  • 158,255
  • 15
  • 216
  • 276
2

You can use guava Splitter:

String str = "BAT4M0ABBB4M0ABOH4M0CCHI4U0AENG4U0DMDM4U0B"
Iterable<String> parts = Splitter.fixedLength(7).split(str);
Алексей
  • 1,847
  • 12
  • 15
0

use string.substring(i,j); i is the first character and j is the last character. It's easy to create a loop that goes through the whole string

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116