Java API将字符串拆分为不同长度的子字符串

时间:2022-02-08 21:41:47

I have a input string of known length. I have to split this input string in 30 substrings of different lengths. For ex:-

我有一个已知长度的输入字符串。我必须将此输入字符串拆分为30个不同长度的子字符串。例如: -

Input string: "abcdefghijklmnopqrstuvwxyz"
Output string: "a","bcd","efgij","kl","mnopqrstu","v","wx","yz"

I wanted to know if there is some sort of API or way where I can provide the lengths, based on which I want to split the string, and get the output at one go instead of multiple steps.

我想知道是否有某种API或方式我可以提供长度,根据我想要分割字符串,并一次性获得输出而不是多个步骤。

Any help will be highly appreciated.

任何帮助将受到高度赞赏。

2 个解决方案

#1


1  

No need for an API. It took just 1 minute to write a method to do that:

不需要API。编写一个方法只用了1分钟就可以了:

public static List<String> splitString(String inputString, int... lengths) {

    List<String> substrings = new ArrayList<String>();

    int start = 0;
    int end = 0;

    for(int length : lengths) {

        start = end;
        end = start + length;

        String substring  = inputString.substring(start, end);
        substrings.add(substring);
    }

    return substrings;
}

Calling splitString("abcdefghi", 3, 4, 2) will produce : [abc, defg, hi]

调用splitString(“abcdefghi”,3,4,2)将产生:[abc,defg,hi]

#2


2  

One way which works out of the box is using RegEx. Here's an example that splits it into 4, 3, 5, 2 and the rest of the string (the first value of the mathed groups is the entire string, that's why I started from 1):

开箱即用的一种方法是使用RegEx。这是一个将它分成4,3,5,2和其余字符串的示例(mathed组的第一个值是整个字符串,这就是我从1开始的原因):

String input = "abcdefghijklmnopqrstuvwxyz";
Matcher m = Pattern.compile("^(.{4})(.{3})(.{5})(.{2})(.*)").matcher(input);
if (m.matches()) {
    for (int i = 1; i <= m.groupCount(); i++) {
        System.out.println(m.group(i));
    }
}

however this is much slower than splitting the string using a simple for, using the dimensions given. You need to take into account speed and quality over ease of use.

然而,这比使用给定的尺寸使用简单的for分割字符串慢得多。您需要考虑速度和质量而不是易用性。

More about regular expressions: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

有关正则表达式的更多信息:https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

#1


1  

No need for an API. It took just 1 minute to write a method to do that:

不需要API。编写一个方法只用了1分钟就可以了:

public static List<String> splitString(String inputString, int... lengths) {

    List<String> substrings = new ArrayList<String>();

    int start = 0;
    int end = 0;

    for(int length : lengths) {

        start = end;
        end = start + length;

        String substring  = inputString.substring(start, end);
        substrings.add(substring);
    }

    return substrings;
}

Calling splitString("abcdefghi", 3, 4, 2) will produce : [abc, defg, hi]

调用splitString(“abcdefghi”,3,4,2)将产生:[abc,defg,hi]

#2


2  

One way which works out of the box is using RegEx. Here's an example that splits it into 4, 3, 5, 2 and the rest of the string (the first value of the mathed groups is the entire string, that's why I started from 1):

开箱即用的一种方法是使用RegEx。这是一个将它分成4,3,5,2和其余字符串的示例(mathed组的第一个值是整个字符串,这就是我从1开始的原因):

String input = "abcdefghijklmnopqrstuvwxyz";
Matcher m = Pattern.compile("^(.{4})(.{3})(.{5})(.{2})(.*)").matcher(input);
if (m.matches()) {
    for (int i = 1; i <= m.groupCount(); i++) {
        System.out.println(m.group(i));
    }
}

however this is much slower than splitting the string using a simple for, using the dimensions given. You need to take into account speed and quality over ease of use.

然而,这比使用给定的尺寸使用简单的for分割字符串慢得多。您需要考虑速度和质量而不是易用性。

More about regular expressions: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

有关正则表达式的更多信息:https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html