用java如何将一个字符串string=“aaabbbcccdddd”,输出为:aaa,bbb,ccc,ddd

2025-01-01 19:54:08
推荐回答(2个)
回答1:

String[] str1 = {"aaa","bbb","ccc"};
String[] str2 = {"bbb","ddd"};
String[] str3 = {"eee","fff"};
//运算开始:
Map isH = new HashMap();
for(String s:str1){
if(isH.get(s)==""){continue;}
isH.put(s,"");
}
for(String s:str2){
if(isH.get(s)==""){continue;}
isH.put(s,"");
}
for(String s:str3){
if(isH.get(s)==""){continue;}
isH.put(s,"");
}
String[] toStr = (String[])isH.keySet().toArray();

回答2:

string str="aaabbbcccddd";
char[] stringArr = str.toCharArray();
for (int i = 0; i < 4; i++)
{
string strs = null;
for (int j = i*3; j < (i+1)*3; j++)
{
strs += stringArr[j];
}
system.out.printf(strs);
}