我只是想知道这是一个 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/