2017-02-10 37 views
6

我想从字符串截断最多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个字符。

+0

怎么样:如果原始字符串有60位(第61个字符)的字符(这意味着你要砍掉某个词或字开头),搜索回来,其中包括59位(第60个字符),并在找到空间时停止。在该位置切断弦。 – slipperyseal

+1

@SlipperySeal如果您有答案,请写下 – smac89

+0

@SlipperySeal最多可以管理2到3个字符,也可以是63或最多57个字符。 –

回答

1

如果原始字符串在位置60(第61个字符)处有一个字符,表示您要剪切一个字或开始一个字,请从位置59(第60个字符)开始搜索并停止,你找到一个空间。然后我们可以在该位置对字符串进行子串处理。如果字符串不超过60个字符,我们就按原样返回。

public void truncateTest() { 
    System.out.println(truncateTo("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. ", 60)); 
    System.out.println(truncateTo("Bangladesh's first day.", 60)); 
    System.out.println(truncateTo("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.", 60)); 
} 

public String truncateTo(String originalText, int len) { 
    if (originalText.length() > len) { 
     if (originalText.charAt(len) != ' ') { 
      for (int x=len-1;x>=0;x--) { 
       if (Character.isWhitespace(originalText.charAt(x))) { 
        return originalText.substring(0, x); 
       } 
      } 
     } 
     // default if none of the conditions are met 
     return originalText.substring(0, len); 
    } 
    return originalText; 
} 

结果...

Bangladesh's first day of Test cricket on Indian soil has 
Bangladesh's first day. 
They end the day having conceded 71 runs in the last 10 

我觉得我得到了我的+1/-1索引逻辑吧:)

综上所述印度的打击,Pujara是耐心的缩影, Vijay的投篮遭到轻视,队长Kohli在印度队完全统治的情况下表现出极度蔑视的态度。

+0

Wayyy复杂 –

+0

@NickZiebert是的,我同意。想要问你是否有更好的主意,你可以:)让我打电话给我的“教学助手”(不该做什么:) – slipperyseal

+0

@NickZiebert我想你想要一个lastIndexOf在那里 - 但它应该处理不在所有的空间 – slipperyseal

4

您可以使用正则表达式这一点,占用60个字符,并在字边界结束:

Pattern pattern = Pattern.compile("(.{1,60})(\\b|$)(.*)"); 
Matcher m = pattern.match(originalText); 
If (m.matches()) 
    afterOptimized = m.group(1); 

或者,在一个循环:

Pattern pattern = Pattern.compile("\\s*(.{1,60})(\\b|$)"); 
Matcher m = pattern.matcher(originalText); 
int last = 0; 
while (m.find()) { 
    System.out.println(m.group(1)); 
    last = m.end(); 
} 
if (last != originalText.length()) 
    System.out.println(originalText.substring(last)); 

您可能要替换\b\s如果您只想在白色空间换行而不是字边界(可能会在逗号,点等之前换行)。

+0

奈瑟奈西。我想一定有一种模式匹配的方式,确实有 – smac89

+0

肯定比我的回答更干净:) – slipperyseal

+0

这对我不起作用。我从来没有用过{1,60}的正则表达式。 –

-1

假设你的文本有两两句话之间的空间,只是切割文本,并检查炭末+结束字符之前+结束字符后,以确定哪些我们需要削减:

if (char[i] != ' ') { 
    if(i+1 == length || (i+1 < length && char[i+1] == ' ')) 
     return mString; // [I'm loser] bla ==> [I'm loser] 
    if(i-1 > -1 && char[i-1] == ' ') 
     return subHeadString(mString, 2); // return mString which has length = length - 2, ex: [I'm loser b]la ==> [I'm loser] 
    return findBackStringWithSpace(mString, i); // coming back until has space char and return that sub string 
// [I'm loser bl]a ==> [I'm loser] 
} else { 
    return mString; 
} 
+0

最好为downvote提供评论,以便人们可以改进/正确答案。 –

0
int cutoff = originalText.substring(0,60).lastIndexOf(" "); 
String afterOptimized = originalText.substring(0, cutoff); 

prints this: "Bangladesh's first day of Test cricket on Indian soil has" 

为什么人们喜欢过分复杂的答案或答案,甚至不编译?

0

String originalText =“孟加拉国在印第安土壤上的第一天测试板球并不是一件好事,他们在最后10场比赛中失去71次比赛,这意味着他们已经盯着总共356次了。 M Vijay在本赛季的第九个测试世纪和第三个测试中表现稳定而乏味。“

//trim the string to 60 characters 

String trimmedString = originalText.substring(0, 60); 

//re-trim if we are in the middle of a word and to get full word instead of brolken one 

String result=trimmedString.substring(0, Math.min(trimmedString.length(), trimmedString.lastIndexOf(" "))); 

System.out.println(result);