在 Java 中将字符串拆分为 n 长度的块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4097577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:36:22  来源:igfitidea点击:

Splitting a string into n-length chunks in Java

javastringsplit

提问by Jonik

Possible Duplicate:
Split string to equal length substrings in Java

可能的重复:
在 Java 中将字符串拆分为等长的子字符串

Given the following utility method I have:

鉴于我有以下实用方法:

/**
 * Splits string <tt>s</tt> into chunks of size <tt>chunkSize</tt>
 *
 * @param s the string to split; must not be null
 * @param chunkSize number of chars in each chuck; must be greater than 0
 * @return The original string in chunks
 */
public static List<String> splitInChunks(String s, int chunkSize) {
    Preconditions.checkArgument(chunkSize > 0);
    List<String> result = Lists.newArrayList();
    int length = s.length();
    for (int i = 0; i < length; i += chunkSize) {
        result.add(s.substring(i, Math.min(length, i + chunkSize)));
    }
    return result;
}

1) Is there an equivalent method in any common Java library(such as Apache Commons, Google Guava) so I could throw it away from my codebase? Couldn't find with a quick look. Whether it returns an array or a List of Strings doesn't really matter.

1)在任何常见的 Java 库(例如 Apache Commons、Google Guava )中是否有等效的方法,以便我可以将它从我的代码库中扔掉?快速浏览找不到。它是返回数组还是字符串列表并不重要。

(Obviously I wouldn't add dependency to some huge framework just for this, but feel free to mention any common lib; maybe I use it already.)

(显然,我不会为此添加对某些大型框架的依赖,但可以随意提及任何常见的库;也许我已经使用了它。)

2) If not, is there some simpler and cleaner way to do this in Java?Or a way that is strikingly more performant? (If you suggest a regex-based solution, please also consider cleanness in the sense of readability for non regex experts... :-)

2)如果没有,在Java中是否有一些更简单,更干净的方法来做到这一点?还是一种性能明显更高的方式?(如果您建议基于正则表达式的解决方案,还请考虑非正则表达式专家在可读性方面的清洁度...... :-)

Edit: this qualifies as a duplicate of the question "Split string to equal length substrings in Java" because of this Guava solutionwhich perfectly answers my question!

编辑:这符合问题“在 Java 中将字符串拆分为相等长度的子字符串”的重复项,因为此番石榴解决方案完美地回答了我的问题!

采纳答案by Sean Patrick Floyd

You can do this with Guava's Splitter:

你可以用 Guava 的Splitter做到这一点:

 Splitter.fixedLength(chunkSize).split(s)

...which returns an Iterable<String>.

...返回一个Iterable<String>.

Some more examples in this answer.

此答案中的更多示例。

回答by The Archetypal Paul

Mostly a duplicate of Split Java String in chunks of 1024 byteswhere the idea of turning it into a stream and reading N bytes at a time would seem to meet your need?

大多数情况下,Split Java String的副本以1024 字节的块为单位,其中将其转换为流并一次读取 N 个字节的想法似乎可以满足您的需要?

Here is a way of doing it with regex(which seems a bit of a sledgehammer for this particular nut)

这是使用正则表达式的一种方法(对于这个特定的坚果似乎有点大锤)