如何拆分给定的字符串String s =“ agshshdhdbdhsusjdidjdjdjfjfjrywoetdmfhd”;

时间:2019-12-21 05:26:46

标签: java string selenium indexing split

我的字符串是
String s="agshshdhdbdhsusjdidjdjdjfjfjrywoetdmfhd"; 我们必须通过

将其拆分为多个子字符串

0-45-89-12等处拆分出来...

1 个答案:

答案 0 :(得分:0)

这是示例代码。 希望能帮助到你。

    public static void main(String[] args) {
        String s= "agshshdhdbdhsusjdidjdjdjfjfjrywoetdmfhd";
        List<String> list = splitIntoEqualPartition(s, 4);
        for (String a: list) {
            System.out.println(a);
        }
    }

    public static List<String> splitIntoEqualPartition(String str, int partitionSize) {
        if (str == null) {
            return null;
        }

        List<String> partitions = new ArrayList<>();
        while (str.length() >= partitionSize) {
                partitions.add(str.substring(0, partitionSize));
                str = str.substring(partitionSize);
        }

        if (str.length() > 0) {
            partitions.add(str);
        }
        return partitions;
    }