我有一个String
数组
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;
}
答案 0 :(得分:6)
你可以做到
String[] array = student[0].split("(?<=\\G.{7})");
\G
是与上一个匹配项匹配的元字符,因此(?<=\\G.{7})
匹配空字符串后跟任意7个字符
答案 1 :(得分:2)
您可以使用guava Splitter
:
String str = "BAT4M0ABBB4M0ABOH4M0CCHI4U0AENG4U0DMDM4U0B"
Iterable<String> parts = Splitter.fixedLength(7).split(str);
答案 2 :(得分:0)
使用string.substring(i,j); 我是第一个角色,j是最后一个角色。 创建一个遍历整个字符串的循环
很容易