我想从字符串截断最多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
但是我的要求是不会削减在between.How的话做,我知道有完整的单词或不经过60个字符。
怎么样:如果原始字符串有60位(第61个字符)的字符(这意味着你要砍掉某个词或字开头),搜索回来,其中包括59位(第60个字符),并在找到空间时停止。在该位置切断弦。 – slipperyseal
@SlipperySeal如果您有答案,请写下 – smac89
@SlipperySeal最多可以管理2到3个字符,也可以是63或最多57个字符。 –