Java在索引处分割字符串而不切割单词

标签 java arrays string

我只是想知道这是一个 API或者一些简单快捷的分割方法String在给定索引 String[] array但如果该索引处有一个单词,则将其放入其他字符串。

假设我有一个字符串:"I often used to look out of the window, but I rarely do that anymore"

该字符串的长度是68,我必须将其剪切为36,即在给定的句子n中,但现在它应该在 the 处分割单词,以便数组为 ["I often used to look out of the", "window, but I rarely do that anymore"] .

如果新句子长于36,那么它也应该被分割,所以如果我有更长的句子:"I often used to look out of the window, but I rarely do that anymore, even though I liked it"
将是["I often used to look out of the", "window, but I rarely do that anymore", ",even though I liked it"]

最佳答案

这是一个老式的、非流、非正则表达式的解决方案:

public static List<String> chunk(String s, int limit) 
{
    List<String> parts = new ArrayList<String>();
    while(s.length() > limit)
    {
        int splitAt = limit-1;
        for(;splitAt>0 && !Character.isWhitespace(s.charAt(splitAt)); splitAt--);           
        if(splitAt == 0) 
            return parts; // can't be split
        parts.add(s.substring(0, splitAt));
        s = s.substring(splitAt+1);
    }
    parts.add(s);
    return parts;
}

这不会修剪分割点两侧的额外空格。此外,如果字符串无法拆分,因为它的前 limit 字符中不包含任何空格,那么它会放弃并返回部分结果。

测试:

public static void main(String[] args)
{
    String[] tests = {
            "This is a short string",
            "This sentence has a space at chr 36 so is a good test",
            "I often used to look out of the window, but I rarely do that anymore, even though I liked it",
            "I live in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
    };

    int limit = 36;
    for(String s : tests)
    {
        List<String> chunks = chunk(s, limit);
        for(String st : chunks)
            System.out.println("|" + st + "|");
        System.out.println();
    }
}

输出:

|This is a short string|

|This sentence has a space at chr 36|
|so is a good test|

|I often used to look out of the|
|window, but I rarely do that|
|anymore, even though I liked it|

|I live in|

关于Java在索引处分割字符串而不切割单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52393269/

相关文章:

java - Hibernate5 update only collection 多对多关系

PHP MYSQL 消除表中的逗号

c - 使用 strcpy 数组输入错误

java - 递归验证字符串是否是有效的前缀表达式?

javascript - 连接与空字符串连接

java - 多模块项目的基于 Spring MVC 注解的配置

java - 空点异常...不知道为什么

java - 通过反射调用不同包中的类的公共(public)方法

arrays - 从数组中删除套接字工作不正确

java - 如何限制单行打印的字符串数量