@Test public void test1(){ String content1 = "AAAAAAAAAAAAAAAAAAAAAAAAttBffgfaaddddddsCDaaaBBBBdddfdsgggggg"; String result = yasuo(content1); System.out.println(result); } public String yasuo(String content){ content = content +" "; StringBuilder builder = new StringBuilder(); int time = 1; for(int i=0;i<content.length()-1;i++){ String s = content.charAt(i)+""; String next = content.charAt(i+1)+""; if(s.matches("\"^[0-9]*$")){ builder.append(s); } if(s.equals(next)){ time+=1; }else{ if(time!=1){ builder.append(time+s); time = 1; continue; } builder.append(s); } } return builder.toString(); }
打印结果: