【发布时间】:2017-02-10 02:38:08
【问题描述】:
我想从字符串中截断最多 60 个字符的子字符串,但也想在子字符串中获取完整的单词。这是我正在尝试的。
String originalText =" Bangladesh's first day of Test cricket on Indian soil has not been a good one. They end the day having conceded 71 runs in the last 10 overs, which meant they are already staring at a total of 356. M Vijay was solid and languid as he made his ninth Test century and third of the season. ";
String afterOptimized=originalText.substring(0, 60);
System.out.println("This is text . "+afterOptimized);
这是输出
This is text . Bangladesh's first day of Test cricket on Indian soil has n
但是我的要求是不要把中间的单词删掉。我怎么知道60个字符之后有没有完整的单词。
【问题讨论】:
-
怎么样:如果原始字符串在位置 60(第 61 个字符)有一个字符(意味着你要剪切一个单词,或者一个单词开始),从位置开始搜索并包括位置59(第 60 个字符)并在找到空格时停止。在那个位置剪断字符串。
-
@SlipperySeal 如果你有答案,写出来
-
@SlipperySeal 将管理最多 2 或 3 个字符,可以是 63 或最多 57。