假设字符串为 A|B|C|D|E|F|G|H|aaa 处理后获取 A|B|,C|D|E|,F|G|H|,aaa

/**
 * @author: Avery
 * @description: 假设字符串为 A|B|C|D|E|F|G|H|aaa
 *               处理后获取 A|B|,C|D|E|,F|G|H|,aaa
 * @date: 2017年6月27日
 */
public class StringTest {
public static void main(String[] args) {
 StringBuilder str=new StringBuilder("A|B|C|D|E|F|G|H|aaa");  //原字符串
 int j=0;
 for(int i=str.length()-1;i>=0;i--){
  if(String.valueOf(str.charAt(i)).matches("[a-zA-Z]")){//判断取出来的字符是不是字母
   j++;
   if(j%3==0)                           //每读到3个字母加一个逗号
    str.insert(i, ',');
  }
 }
 System.out.println(str);
}
}

你可能感兴趣的:(基础)