Trim String in Java, keeping the full word

I need to trim a string in java so that:

A quick brown fox jumps over a dog-manhole.

becomes

Quick brown ...

In the above example, I truncate to 12 characters. If I just use a substring, I would get:

Quick ...

I already have a method for this, using a substring, but I wanted to know what is the fastest (most efficient) way to do this, because a page can have many trim operations.

The only way I can think of is to split the string into spaces and put it back together until its length has passed the given length. Is there another way? Perhaps in a more efficient way that I can use the same method for soft trim, where I save the last word (as shown in the example above) and hard trim, which is pretty much a substring.

Thanks,

+8
java string substring trim
source share
7 answers

Below is the method that I use to trim long lines in my web folders. The soft boolean , as you put it, if set to true , the last word will be saved. This is the most concise way to do this so that I can come up with what uses a StringBuffer, which is much more efficient than re-creating a string that is immutable.

 public static String trimString(String string, int length, boolean soft) { if(string == null || string.trim().isEmpty()){ return string; } StringBuffer sb = new StringBuffer(string); int actualLength = length - 3; if(sb.length() > actualLength){ // -3 because we add 3 dots at the end. Returned string length has to be length including the dots. if(!soft) return escapeHtml(sb.insert(actualLength, "...").substring(0, actualLength+3)); else { int endIndex = sb.indexOf(" ",actualLength); return escapeHtml(sb.insert(endIndex,"...").substring(0, endIndex+3)); } } return string; } 

Update

I changed the code so that ... was added to the StringBuffer to prevent unnecessary String creation implicitly, which is slow and wasteful.

Note. escapeHtml is a static import from apache commons:

import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;

You can remove it, and the code should work the same.

+11
source share

Here is a simple, regex-based, single-line solution:

 str.replaceAll("(?<=.{12})\\b.*", "..."); // How easy was that!? :) 

Explanation:

  • (?<=.{12}) is a negative appearance that states that at least 12 characters remain to the left of the match, but it is a non-convertible (i.e. null) match
  • \b.* matches the first word boundary (after at least 12 characters - above) to the end

This is replaced by "..."

Here's the test:

 public static void main(String[] args) { String input = "The quick brown fox jumps over the lazy dog."; String trimmed = input.replaceAll("(?<=.{12})\\b.*", "..."); System.out.println(trimmed); } 

Output:

 The quick brown... 
+7
source share

Please try the following code:

 private String trim(String src, int size) { if (src.length() <= size) return src; int pos = src.lastIndexOf(" ", size - 3); if (pos < 0) return src.substring(0, size); return src.substring(0, pos) + "..."; } 
+4
source share

Try to find the last place in the space that is at a position of less than or greater than 11 and cuts the line there by adding "...".

0
source share

Your requirements are not clear. If you have problems formulating them in a natural language, it is not surprising that it will be difficult for them to translate into a computer language such as Java.

โ€œsave last wordโ€ means that the algorithm will know what the word is, so you should say that first. Splitting is the way to do it. A grammar scanner / parser is another.

I will worry about making it work before I take care of myself. Make it work, measure it, and then see what you can do with performance. Everything else is speculation without data.

0
source share

What about:

 mystring = mystring.replaceAll("^(.{12}.*?)\b.*$", "$1..."); 
0
source share

I use this hack: suppose the cropped string should be 120:

 String textToDisplay = textToTrim.substring(0,(textToTrim.length() > 120) ? 120 : textToTrim.length()); if (textToDisplay.lastIndexOf(' ') != textToDisplay.length() &&textToDisplay.length()!=textToTrim().length()) { textToDisplay = textToDisplay + textToTrim.substring(textToDisplay.length(),textToTrim.indexOf(" ", textToDisplay.length()-1))+ " ..."; } 
0
source share

All Articles