如何把一个字符串按指定长度分割成字符串数组:
如:
String name=new String("kensingle");如:按4个字符分:int n=name.length;  得到9位double r=new Integer(n).doubleValue()/4;    得到2.25如:得到有小数位2.25, 就取3String [] nameList=new String[3];nameList[0]="kens";
nameList[1]="ingl";
nameList[2]="e";比较菜!求大家帮忙!一时没思路!

解决方案 »

  1.   

    String str = new String("kensingle");String [] nameList = null;int iLen=0,i;iLen = (int)Math.ceil(str.length/4.0);nameList = new String[iLen];for(i=0;i<iLen;i++)
        nameList[i]=str.subString(i*4,i*4+4);
      

  2.   

    String str = new String("kensingle");String [] nameList = null;int iLen=0,i;iLen = (int)Math.ceil(str.length()/4.0);nameList = new String[iLen];for(i=0;i<iLen;i++)
        if(i!=iLen-1)
            nameList[i]=str.subString(i*4,i*4+4);
        else nameList[i]=str.subString(i*4,str.length());==========
    上面有点错误!没考虑空串的情况~
      

  3.   

    int arrLen = (str.length() + 4 - 1) / 4;
      

  4.   

    class StringDemo{
    public static void main(String[] args){
    String name = new String("kensingle");
    int n = name.length();
    double r = new Integer(n).doubleValue() / 4;
    int num = 0;
    if(r > (int)r){
    num = (int)r + 1;
    }
    String str[] = new String[num]; int j = 0;
    int k = 0;
    for(int i = 0; i < num; i ++){
    k = j + 4;
    if(k <= name.length()){
    str[i] = name.substring(j, k);
    j = k;
    }
    else{
    str[i] = name.substring(j, name.length());
    }
    System.out.println(str[i]);
    }
    }
    }ok
      

  5.   

    public static String[] splitString(String str, int gap) {
            if (gap < 0) {
                return null;
            }
            
            int length = str.length();
            int num = (length - 1)/gap + 1;
            
            String[] result = new String[num];
            for(int i = 0, begin = 0, end = 0; i < num; i++) {
                end = begin + gap < length ? begin + gap : length ;
                result[i] = str.substring(begin, end);
                begin = end;
                
            }
            
            return result;
        }