开发者

Split string into array by K?

I am trying to figure out how to split a string into an 2d array based on k.

I know how to split it into individual strings or by regex.

How do you go about splitting it like this;

String text = "thisismyp";

// Result (where k = 3): 
char[][] = {
  {'t','h','i'},
  {'s','i','s'},
  {'m','y'开发者_如何学运维,'p'}
}; 


.{k} should work where you have to compose the regex yourself inserting the value of k into it.


The regex is:

(.{3})

Nice and simple. You can add case insensitivity, newline handling, etc, according to your use case.


I haven't tested it, so maybe you need to change a little bit, but I think that this will work:

public String[] split(String str, int k)
{
    String[] a = new String[Math.ceil((double)str.length() / (double)k)];
    for (int i = 0; i < str.length(); i += k)
    {
        a[i / k] = str.substring(i, Math.min(i + k, str.length()));
    }
    return a;
}

If you want it really as a char matrix, use something like this:

public char[][] split(String str, int k)
{
    char[][] a = new char[Math.ceil((double)str.length() / (double)k)][0];
    for (int i = 0; i < str.length(); i += k)
    {
        String part = str.substring(i, Math.min(i + k, str.length()));
        a[i / k] = part.toCharArray();
    }
    return a;
}


If you don't want to use a regex you can brute-force it. Brute force is faster but in your case I wouldn't worry about performance. Still if anyone lands on this page and is not using a language with easy regex support; here is the fast Java implementation, embedded within a complete application you can compile and run.

import java.util.Arrays;
public class Split {

    private static int K = 3;

    /**
     * Splits a string into a 2-D array of chars.  An array or list of strings would
     * be better, but <strong>the OP asked for an array of array of chars!!!
     */
    public static char[][] split(String s) {
        char[][] result = new char[(s.length() + (K-1)) / K][K];
        int row = 0, col = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            result[row][col] = c;
            if (col == K - 1) {row++; col = 0;} else col++;
        }
        return result;
    }

    // Sorry about the hideous copy/paste
    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(split("")));
        System.out.println(Arrays.deepToString(split("1")));
        System.out.println(Arrays.deepToString(split("12")));
        System.out.println(Arrays.deepToString(split("123")));
        System.out.println(Arrays.deepToString(split("1234")));
        System.out.println(Arrays.deepToString(split("12345")));
        System.out.println(Arrays.deepToString(split("123456")));
        System.out.println(Arrays.deepToString(split("1234567")));
        System.out.println(Arrays.deepToString(split("12345678")));
        System.out.println(Arrays.deepToString(split("123456789")));
        System.out.println(Arrays.deepToString(split("1234567890")));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜