2014-01-12 25 views
2

我有一个String阵列如何在java中将一个没有空格的字符串分成多个单词?

String student [] = {"BAT4M0ABBB4M0ABOH4M0CCHI4U0AENG4U0DMDM4U0B"} 

我需要拆分此为每7个字符。 它应该看起来像这样:

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

这是我的代码到目前为止。

 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; 
     } 
+1

你希望你的最终结果是一个字符串数组吗? –

+3

Plz看到一个类似的线程 http://stackoverflow.com/questions/12295711/split-a-string-at-every-nth-position –

回答

6

你可以做

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

\G是元字符匹配的最后一场比赛这么(?<=\\G.{7})一个空字符串后跟任意7个字符匹配

+4

你可以请添加一个正则表达式的解释吗? –

+0

你好我也想看到它 –

+0

我不得不提出一个不同的问题,因为我忘记了这个字符串已经存在于一个数组中。 – user3161311

0

use string.substring(i,j); 我是第一个字符,j是最后一个字符。 可以很容易地创建一个循环,在整个串去

2

您能给我们e番石榴Splitter

String str = "BAT4M0ABBB4M0ABOH4M0CCHI4U0AENG4U0DMDM4U0B" 
Iterable<String> parts = Splitter.fixedLength(7).split(str); 
相关问题