How to trim a string of a certain length, but include full words after truncation

I want to trim a substring from a string to 60 characters, but also want to get full words in a substring. Here is what I am trying.

String originalText =" Bangladesh 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); 

It displays here

 This is text . Bangladesh first day of Test cricket on Indian soil has n 

However, my requirement is not to cut the words between them. How do I know that there are full words or not after 60 characters.

+6
source share
5 answers

You can use the regular expression for this, taking up to 60 characters and ending at the word boundary:

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

Or, in a loop:

 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)); 

You can replace \b with \s if you want to wrap only in empty space instead of the word boundary (which can wrap before a comma, periods, etc.).

+4
source

If the source line has a character at position 60 (char 61st), that is, you are going to cut the word or start word, look back and turn on position 59 (char 60th), and stop when you find a place. Then we can fine-tune the string at that location. If the string is no longer than 60 characters, we simply return it as is.

 public void truncateTest() { System.out.println(truncateTo("Bangladesh 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 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; } 

Results...

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

I think I got the logic of the index + 1 / -1 :)

Summing up the Indian batting, Pujara was an embodiment of patience, Vijay's shots were despised, and the skipper Kolya crowned this with a manifestation of complete contempt in what turned out to be the complete dominance of the Indian team.

+1
source
  int cutoff = originalText.substring(0,60).lastIndexOf(" "); String afterOptimized = originalText.substring(0, cutoff); prints this: "Bangladesh first day of Test cricket on Indian soil has" 

Why do people redirect overly complex answers or answers that don't even compile?

0
source

String originalText = "Bangladesh the first day of cricket test on Indian soil was not good. They end the day by skipping 71 runs in the last 10 seconds, which means they are already watching a total of 356. M Vijay was hard and lethargic when he made his ninth test century and a third of the season. ";

 //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); 
0
source

Suppose your texts have a space between the two words, just cutting out the text and checking if the end of char + ends to end of char + after end of char to determine what we need to cut:

 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; } 
-one
source

Source: https://habr.com/ru/post/1014862/


All Articles