用K将字符串拆分成数组?

时间:2023-01-16 21:31:11

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

我试图找出如何基于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'}
}; 

4 个解决方案

#1


0  

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

。{k}应该在你必须自己编写正则表达式的地方工作,并将k的值插入其中。

#2


0  

The regex is:

正则表达式是:

(.{3})

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

很好,很简单。您可以根据用例添加不区分大小写,换行处理等。

#3


0  

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:

如果你真的想要它作为一个char矩阵,使用这样的东西:

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;
}

#4


0  

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.

如果你不想使用正则表达式,你可以强制它。蛮力更快但在你的情况下我不会担心性能。仍然如果有人登陆此页面并且没有使用具有简单正则表达式支持的语言;这是快速的Java实现,嵌入在一个完整的应用程序中,您可以编译和运行。

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")));
    }
}

#1


0  

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

。{k}应该在你必须自己编写正则表达式的地方工作,并将k的值插入其中。

#2


0  

The regex is:

正则表达式是:

(.{3})

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

很好,很简单。您可以根据用例添加不区分大小写,换行处理等。

#3


0  

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:

如果你真的想要它作为一个char矩阵,使用这样的东西:

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;
}

#4


0  

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.

如果你不想使用正则表达式,你可以强制它。蛮力更快但在你的情况下我不会担心性能。仍然如果有人登陆此页面并且没有使用具有简单正则表达式支持的语言;这是快速的Java实现,嵌入在一个完整的应用程序中,您可以编译和运行。

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")));
    }
}