2014-01-12 27 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); 
相關問題