3

How can i truncate a text whithout cut in the middle of a word?

For exemple, I have the string :

"A totally fresh and new approach to life itself emerges. Once again, you’re off to a good start, willing to do that little bit extra in your result-oriented frame of mind. It’s not the amount of effort but the results that matter to you. You also gain much in the depth of the romance and emotional bonds in your life. This is a good time for self-improvement programs or philanthropy, alms-giving and charity."

If i cut it, i want to cut like this :

"A totally fresh and new approach to life itself emerges. Once again, you’re off to a good start, willing to do that little bit extra in your result-oriented frame of mind. It’s not the amount of effort but the results that matter to you. You also gain much in the depth of the romance and"

And not :

"A totally fresh and new approach to life itself emerges. Once again, you’re off to a good start, willing to do that little bit extra in your result-oriented frame of mind. It’s not the amount of effort but the results that matter to you. You also gain much in the depth of the romance and emoti"

7bluephoenix
  • 958
  • 7
  • 18
Bitu Patel
  • 131
  • 2
  • 11
  • 2
    Where do you want to cut ? How do you determine that ? Do you know the index where to cut ? – AllTooSir Jun 19 '13 at 10:09
  • 1
    This question has been asked before, http://stackoverflow.com/questions/4212675/wrap-the-string-after-a-number-of-character-word-wise-in-java – DevZer0 Jun 19 '13 at 10:09
  • If so just check at the index and move it backwards or forwards to the next whitespace. – selig Jun 19 '13 at 10:09
  • Shouldn't `ellipsed` do this for you? – Aleks G Jun 19 '13 at 10:10
  • I think he is asking about `substring()` or wrapping ! I'm confused. – AllTooSir Jun 19 '13 at 10:10
  • i have to fit it in the textview.like this way for (int i = 0; i < totalLine-2; i++) { // Get No of characters fit in that textView int number= holder.txtitem_description.getPaint().breakText(textToBeShown, 0, textToBeShown.length(), true, holder.txtitem_description.getWidth()-holder.txtitem_description.getPaddingLeft()-holder.txtitem_description.getPaddingRight(), null); // Update the text to show next textToBeShownintext+=textToBeShown.substring(0, number); textToBeShown = textToBeShown.substring(number,textToBeShown.length()); } – Bitu Patel Jun 19 '13 at 10:35
  • @the new ldiot so i know the index where to cut. – Bitu Patel Jun 19 '13 at 10:36

3 Answers3

12

In this method pass your string and last index till you want to trancate.

public String truncate(final String content, final int lastIndex) {
    String result = content.substring(0, lastIndex);
    if (content.charAt(lastIndex) != ' ') {
        result = result.substring(0, result.lastIndexOf(" "));
    }
    return result;
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
3

WordUtils.wrap(String str, int wrapLength) from Apache Commons.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
0

This will cut the String in the middle (more or less).

public static void main(String[] args) {
    String s = "A totally fresh and new approach to life itself emerges. Once again, you’re off to a good start, willing to do that little bit extra in your result-oriented frame of mind. It’s not the amount of effort but the results that matter to you. You also gain much in the depth of the romance and emotional bonds in your life. This is a good time for self-improvement programs or philanthropy, alms-giving and charity.";
    int middle = s.length() / 2;
    while(s.charAt(middle) != ' ') {
        middle++;
    }
    String start = s.substring(0, middle);
    String end = s.substring(middle, s.length());
    System.out.println(start);
    System.out.println(end);
}
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • Did you try with `String s = "A totally";` ? – Alexis C. Jun 19 '13 at 10:20
  • Nope, but I can imagine the result. It only depends on what you want to achieve. The question is not clear and I just gave a solution. You can then adjust the code. (personally I like more the @Ashish Aggarwal solution) – Enrichman Jun 19 '13 at 10:22