在每 4 个字符处拆分一个字符串?

我有一个字符串,如果可能的话,我必须将其拆分为长度相等的子字符串。我发现这个解决方案只有在字符串长度是 4 的倍数时才有效。

String   myString = "abcdefghijklm";
String[] split = myString.split("(?<=\\G....)");

这将产生:

[abcd, efgh, ijkl, m]

我需要的是拆分“从字符串的末尾”。我想要的输出应该如下所示:

[a, bcde, fghi, jklm]

我如何实现这一目标?


幕布斯7119047
浏览 160回答 3
3回答

汪汪一只猫

这应该这样做:String[] split = myString.split("(?=(....)+$)");// orString[] split = myString.split("(?=(.{4})+$)");它的作用是:仅当空字符串前面有 4 个字符的倍数时才拆分空字符串,直到到达输入结束。当然,这有一个糟糕的运行时间 (O(n^2))。您只需自己拆分即可获得线性运行时间算法。正如@anubhava 所提到的:(?!^)(?=(?:.{4})+$)如果字符串长度是 4 的倍数,以避免出现空结果

米脂

正则表达式对此真的是不必要的。我也不认为这对递归来说是一个好问题。下面是一个 O(n) 的解决方案。public static String[] splitIt(String input, int splitLength){&nbsp; &nbsp; int inputLength = input.length();&nbsp; &nbsp; ArrayList<String> arrayList = new ArrayList<>();&nbsp; &nbsp; int i = inputLength;&nbsp; &nbsp; while(i > 0){&nbsp; &nbsp; &nbsp; &nbsp; int beginIndex = i - splitLength > 0 ? i - splitLength : 0;&nbsp; &nbsp; &nbsp; &nbsp; arrayList.add(0, input.substring(beginIndex, i));&nbsp; &nbsp; &nbsp; &nbsp; i -= splitLength;&nbsp; &nbsp; }&nbsp; &nbsp; return arrayList.toArray(new String[0]);}

HUX布斯

无需使用正则表达式。相反,您可以递归地构建头部字符串列表并返回尾部。import java.util.*;public class StringChunker {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String str = "abcdefghijklm";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(chunk(str, 4)));&nbsp; &nbsp; &nbsp; &nbsp; // [abcd, efgh, ijkl, m]&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(chunk(str, 4, true)));&nbsp; // [a, bcde, fghi, jklm]&nbsp; &nbsp; }&nbsp; &nbsp; public static String[] chunk(String str, int size) throws IllegalArgumentException {&nbsp; &nbsp; &nbsp; &nbsp; return chunk(str, size, false);&nbsp; &nbsp; }&nbsp; &nbsp; public static String[] chunk(String str, int size, boolean reverse) throws IllegalArgumentException {&nbsp; &nbsp; &nbsp; &nbsp; return chunk(str, size, reverse, new ArrayList<String>());&nbsp; &nbsp; }&nbsp; &nbsp; private static String[] chunk(String str, int size, boolean reverse, List<String> chunks) throws IllegalArgumentException {&nbsp; &nbsp; &nbsp; &nbsp; if (size < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("size must be greater than 0");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (str.length() < size) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (reverse) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunks.add(0, str); // Reverse adds to the front of the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunks.add(str); // Add to the end of the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return chunks.toArray(new String[chunks.size()]); // Convert to an array&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String head, tail;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (reverse) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; head = str.substring(str.length() - size, str.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tail = str.substring(0, str.length() - size);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunks.add(0, head);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; head = str.substring(0, size);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tail = str.substring(size);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunks.add(head);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return chunk(tail, size, reverse, chunks);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java