我想在特定长度后分割字符串而不剪切任何单词,不等于字符串

我想在长度为 35 时分割一个字符串,例如“仅卢比 241 和 68”,我想将该字符串分割为 2。我尝试使用此代码来分割该字符串。


String text = "Rupees Two Hundred Forty One and Sixty Eight only";

List<String> parts = new ArrayList<>();

int length = text.length();

for (int i = 0; i < length; i += 35) {

    parts.add(text.substring(i, Math.min(length, i + size)));

但输出是这样的。


[Rupees Two Hundred Forty One and Si, xty Eight only]


但我想像这样分割字符串。


[Rupees Two Hundred Forty One and, Sixty Eight only]


分割字符串时没有切词。该字符串每次都会根据账单金额而变化。


动漫人物
浏览 127回答 4
4回答

胡子哥哥

你可能无法完全做到这一点。但是使用 String.indexOf() 找到从 35 开始的第一个空格。然后使用 substring 方法来划分字符串。&nbsp; &nbsp; &nbsp; String text = "Rupees Two Hundred Forty One and Sixty Eight only";&nbsp; &nbsp; &nbsp; int i = text.indexOf(" ", 35);&nbsp; &nbsp; &nbsp; if (i < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i = text.length();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; String part1 = text.substring(0,i).trim();&nbsp; &nbsp; &nbsp; String part2 = text.substring(i).trim();这是一种替代方法。尚未对边境案件进行全面检查。&nbsp; &nbsp; &nbsp; String[] words = text.split(" ");&nbsp; &nbsp; &nbsp; int k;&nbsp; &nbsp; &nbsp; part1 = words[0];&nbsp; &nbsp; &nbsp; for (k = 1; k < words.length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (part1.length() >= 35 - words[k].length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;part1 += " " + words[k];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (k < words.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;part2 = words[k++];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (k < words.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; part2 += " " + words[k++];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println(part1);&nbsp; &nbsp; &nbsp; System.out.println(part2);

aluckdog

只需在该职位上搜索首选职位即可i+35。需要考虑的一件事是,当没有这样的位置时,即单词超过指定的大小时,会发生什么。以下代码将强制执行大小限制,如果找不到合适的位置,则会在单词中间中断:List<String> parts = new ArrayList<>();int size = 35, length = text.length();for(int i = 0, end, goodPos; i < length; i = end) {&nbsp; &nbsp; end = Math.min(length, i + size);&nbsp; &nbsp; goodPos = text.lastIndexOf(' ', end);&nbsp; &nbsp; if(goodPos <= i) goodPos = end; else end = goodPos + 1;&nbsp; &nbsp; parts.add(text.substring(i, goodPos));}如果中断发生在空格字符处,则该空格将从结果字符串中删除。

慕神8447489

我认为您可以使用while循环来计算包含最后一个空格字符的单词:public static List<String> split(String str, int length) {&nbsp; &nbsp; List<String> res = new ArrayList<>();&nbsp; &nbsp; int prvSpace = 0;&nbsp; &nbsp; int from = 0;&nbsp; &nbsp; while (prvSpace < str.length()) {&nbsp; &nbsp; &nbsp; &nbsp; int pos = str.indexOf(' ', prvSpace + 1);&nbsp; &nbsp; &nbsp; &nbsp; if (pos == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.add(str.substring(from));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prvSpace = str.length();&nbsp; &nbsp; &nbsp; &nbsp; } else if (pos - from < length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prvSpace = pos;&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.add(str.substring(from, prvSpace));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from = prvSpace + 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return res;}演示:in: "RupeesTwoHundredFortyOneandSixtyEightonly"out: ["RupeesTwoHundredFortyOneandSixtyEightonly"]in: "Rupees Two Hundred Forty One and Sixty Eight only"out: ["Rupees Two Hundred Forty One and", "Sixty Eight only"]

喵喵时光机

我找到了使用 Apache commons-lang3 的替代方案:import java.util.Arrays;import org.apache.commons.lang3.StringUtils;import org.apache.commons.lang3.text.WordUtils;class Example {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String text = "Rupees Two Hundred Forty One and Sixty Eight only";&nbsp; &nbsp; &nbsp; &nbsp; String wrappedText = WordUtils.wrap(text, 35, "\n", false);&nbsp; &nbsp; &nbsp; &nbsp; String[] lines = StringUtils.split(wrappedText, "\n");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.asList(lines));&nbsp; &nbsp; &nbsp; &nbsp; // Outputs [Rupees Two Hundred Forty One and, Sixty Eight only]&nbsp; &nbsp; }}注意。如果您的输入有换行符,最好将其删除。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java