gpt4 book ai didi

java - 如何在java中将一个不带空格的字符串分割成多个单词?

转载 作者:行者123 更新时间:2023-12-02 13:15:38 24 4
gpt4 key购买 nike

我有一个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;
}

最佳答案

你可以做

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

\G是与最后一个匹配项匹配的元字符,因此 (?<=\\G.{7})匹配空字符串后跟任意 7 个字符

关于java - 如何在java中将一个不带空格的字符串分割成多个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21081553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com