java - java中如何根据子字符串的长度将字符串转换为子字符串数组?

标签 java regex string

我想根据子字符串的长度将字符串转换为子字符串数组。有没有不使用循环的解决方案? 例如:

"this is a test" -> ["thi","s i","s a"," te","st"]
<小时/>

更新:

更多解决方案在这里:Splitting a string at every n-th character

最佳答案

看看Bart Kiers' example:

System.out.println(java.util.Arrays.toString("this is a test".split("(?<=\\G...)")));

句点“.”的数量表示每个子字符串有多少个字符。

<小时/>

编辑

正如 Mick Mnemonic 指出的,您还可以使用 Kevin Bourrillion's example :

Java does not provide very full-featured splitting utilities, so the Guava libraries do:

Iterable<String> pieces = Splitter.fixedLength(3).split(string);

Check out the Javadoc for Splitter; it's very powerful.

如果你不想使用正则表达式,并且不希望依赖第三方库,你可以使用这个方法来代替,它需要介于 2.80 GHz CPU 中的 89920100113 纳秒(小于一毫秒):

   /**
     * Divides the given string into substrings each consisting of the provided
     * length(s).
     * 
     * @param string
     *            the string to split.
     * @param defaultLength
     *            the default length used for any extra substrings. If set to
     *            <code>0</code>, the last substring will start at the sum of
     *            <code>lengths</code> and end at the end of <code>string</code>.
     * @param lengths
     *            the lengths of each substring in order. If any substring is not
     *            provided a length, it will use <code>defaultLength</code>.
     * @return the array of strings computed by splitting this string into the given
     *         substring lengths.
     */
    public static String[] divideString(String string, int defaultLength, int... lengths) {
        java.util.ArrayList<String> parts = new java.util.ArrayList<String>();

        if (lengths.length == 0) {
            parts.add(string.substring(0, defaultLength));
            string = string.substring(defaultLength);
            while (string.length() > 0) {
                if (string.length() < defaultLength) {
                    parts.add(string);
                    break;
                }
                parts.add(string.substring(0, defaultLength));
                string = string.substring(defaultLength);
            }
        } else {
            for (int i = 0, temp; i < lengths.length; i++) {
                temp = lengths[i];
                if (string.length() < temp) {
                    parts.add(string);
                    break;
                }
                parts.add(string.substring(0, temp));
                string = string.substring(temp);
            }
            while (string.length() > 0) {
                if (string.length() < defaultLength || defaultLength <= 0) {
                    parts.add(string);
                    break;
                }
                parts.add(string.substring(0, defaultLength));
                string = string.substring(defaultLength);
            }
        }

        return parts.toArray(new String[parts.size()]);
    }

关于java - java中如何根据子字符串的长度将字符串转换为子字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549990/

相关文章:

java - 使用正则表达式从输入中解析字符串

algorithm - 字符串比较算法,相关性, "alike"2个字符串是多少

javascript - 从切片方法返回剩余的整个文本

java - 如何从 Android 的内部存储中获取 .mp3 文件

java - 在运行时设置嵌入式 Neo4j 数据库的路径

java - P2P聊天 Atmosphere

c++ - getline(cin,str) 的奇怪输出

java - 推送通知在 Android 中不起作用,代号一

c# - 正则表达式 : extract all words out of quotes

java - 从Java中的字符串中提取数字