当字符串长度为35时,我想将类似于“Rupees Two Hundred Forty One and Sixty Eight only”这样的字符串分成两部分。我尝试使用以下代码来拆分字符串:
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]
在分割字符串时没有断词。每次根据账单金额的不同,字符串也会发生变化。
' '
处拆分文本,还是在任何空白处 - 或者甚至在其他字符(如'-'
)处拆分?如果是这样,这些字符可能应该出现在输出中... 这里存在许多自由度和注意事项... - Marco13