例如有一串字符串:123456123456123456123456123456123456123456123456123456123456(字符串位数不确定)
我要的结果:分解成6个一组,直到没有字符串可以分解了。
123456
123456
123456
123456
123456
123456请各位大哥大姐,赐代码。

解决方案 »

  1.   

    是按顺序的么
    String str = "123456123456123456123456123456123456123456123456123456123456"    char[] c = str.toArray();//方法不记得鸟
        StringBuffer sb = null        
        int num = 1;
         for(int i = 0;i<c.length;i++){
           if(num == 1){
             sb = new StringBuffer(); 
             sb.append(c[i]);
           }
           if(num == 6){
              System.out.println(sb);
              num = 0;
           }
           num++;
        }
      

  2.   

    char[] cs = s.toString();
    int k = 0;
    for(int i = cs.length;i-=6;i>=6) {String tempS = new String(cs,k,i >= 6 ? k + 6, k + i);
    //处理你的tempS
    k += k;//往前步进
    }
      

  3.   

    使用Java正则表达式对字符串分组操作
      

  4.   

    我也写了一个,跟楼上的不同。大家集思广益package com.java.io;public class SplitString { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str = "abcddddddffefadfdd";
    String[] arr = splitStringBySix(str);
    for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
    } } public static String[] splitStringBySix(String str) {
    if (null == str || "".equals(str)) {
    return null;
    }
    // 计算存放数据的数组的长度
    int arrLength = (str.length() % 6 == 0) ? str.length() / 6 : str
    .length() / 6 + 1;
    String[] arr = new String[arrLength]; for (int i = 0; i < arr.length; i++) {
    // 主要是处理,最后的几位不够6位的情况
    if (i == arr.length - 1) {
    arr[i] = str.substring(i * 6);
    }
    arr[i] = str.substring(i * 6, (i + 1) * 6);
    }
    return arr;
    }
    }
      

  5.   

    不好意思。上面代码不小心把else漏掉了package com.java.io;public class SplitString { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str = "abcddddddffefadfd";
    String[] arr = splitStringBySix(str);
    for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
    } } public static String[] splitStringBySix(String str) {
    if (null == str || "".equals(str)) {
    return null;
    }
    // 计算存放数据的数组的长度
    int arrLength = (str.length() % 6 == 0) ? str.length() / 6 : str
    .length() / 6 + 1;
    String[] arr = new String[arrLength]; for (int i = 0; i < arr.length; i++) {
    // 主要是处理,最后的几位不够6位的情况
    if (i == arr.length - 1) {
    arr[i] = str.substring(i * 6);
    }else{
    arr[i] = str.substring(i * 6, (i + 1) * 6);
    }
    }
    return arr;
    }
    }
      

  6.   

    一行就够了
    for (int i=0; i<s.length()/6; i++) 
    System.out.println(s.substring(i*6, i*6+6));
      

  7.   

    短一个
    public class Test {
    public static void main(String[] args) {
    String str = "123456123456123456123456123456123456123456123456123456123456123";
    for (int i = 0; i < str.length(); i += 6) {
    System.out.println(str.substring(i, i + 6 > str.length() ? str.length() : i + 6));
    }
    }
    }
      

  8.   

    new String[6] 去接收然后循环 substring 就行了
      

  9.   

    凑个热闹吧
    public class Test {
        public static void main(String[] args) {
            String str = "123456123456123456123456123456123456123456123456123456123456123";
            for(;str.length()>=6; str=str.substring(6)) {
                System.out.println(str.substring(0, 6));
            }
            if (str.length()>0) {System.out.println(str);}
        }
    }
      

  10.   


    嘿嘿,楼主说6个一组嘛,并没指出最后不够6个的要还是不要。
    如果不要,上面的版本就是唯一正确的。
    如果最后不够6个的仍然要,也只要稍加调整,行数不变for (i=0; i<=s.length()/6; i++)
    System.out.println(s.substring(i*6, Math.min(i*6+6,s.length())));
      

  11.   

    写了一下试了试
    package com.test;public class reallytest { /**
     * @param args
     */
    public static void main(String[] args) {
    String s = "12312231321316546464613215674879";
    if(s.length()%6==0){
    for (int i=0; i<s.length()/6; i++) 
    System.out.println(s.substring(i*6, i*6+6));
    }else{
    for (int i=0; i<s.length()/6; i++) 
    System.out.println(s.substring(i*6, i*6+6));
    System.out.println(s.substring(s.length()/6*6));
    }
    }
    }
      

  12.   

    package com.test;public class reallytest { /**
     * @param args
     */
    public static void main(String[] args) {
    String s = "12312231321316546464613215674879";
    if(s.length()%6==0){
    for (int i=0; i<s.length()/6; i++) 
    System.out.println(s.substring(i*6, i*6+6));
    }else{
    for (int i=0; i<s.length()/6; i++) 
    System.out.println(s.substring(i*6, i*6+6));
    System.out.println(s.substring(s.length()/6*6));
    }
    }
    }
    已通过
      

  13.   

    public static void main(String[] args) {
    String s = "123456123456123456123456123456123456123456123456123456123456";

    String [] ss = s.split("1");

    for(int i = 1; i<ss.length; i++){
    System.out.println("1"+ss[i]);
    }
    }
      

  14.   

    for (i=0; i<=s.length()/6; i++)
        System.out.println(s.substring(i*6, Math.min(i*6+6,s.length())));for (int i=0; i<s.length()/6; i++)
    System.out.println(s.substring(i*6, i*6+6));这两个方案都符合需求,不过我已用另一种方法了。杯具
    public static boolean isNotboolean(StringBuffer str){
    List<String> strList = new ArrayList<String>();
    String tempList="";
    boolean bool = false;
    int start = 0;
    int end = start + 6;
    while (true) {
    if (start >= str.length())
    break;
    String temp = str.substring(start, end);
    strList.add(temp);
    System.out.println(temp);
    start = end;
    end = end + 6;
    if (end >= str.length()) {
    end = str.length();
    }
    }
    for (int i = 0; i < strList.size(); i++) {
    tempList=strList.get(i).toString();
    System.out.println(tempList+"----");
    if("100202".equals(tempList) || "100203".equals(tempList) || "100401".equals(tempList) ||
       "100402".equals(tempList) || "100302".equals(tempList) || "100303".equals(tempList) ||
       "300202".equals(tempList) || "300203".equals(tempList) || "300301".equals(tempList) ||
       "300302".equals(tempList) || "300303".equals(tempList) || "300304".equals(tempList) ||
       "100110".equals(tempList) || "100308".equals(tempList)){
    bool=true;
        }
    }
    if(bool){
    return bool;
    }
    return false;
    }
    }