0

I found a class that does this, and it seems exactly what I need.

public static class StringUtil
{
  public static String limit(String value, int length)
  {
    StringBuilder buf = new StringBuilder(value);
    if (buf.length() > length)
    {
      buf.setLength(length);
      buf.append("…");
    }

    return buf.toString();
  }
}

I have a string called review, and I use that class this way:

StringUtil.limit(review, 50);

It does not seem to be appending.

For what it's worth, it is for an Android app. This is being done in a PostExectute method of AsyncTask inside of a ListFragement.

Am I doing this right?

0

2 Answers 2

1

Is the string you're sending in (review) longer than 50 characters? Note that the function only modifies the result if it's longer than the limit.

You also don't seem to be accepting the return value of the method. This method does NOT modify the argument value (since it can't), it returns a NEW String with the new value.

Try

String newreview = StringUtil.limit( review, 50 );
System.out.println( newreview );

Ok, you asked for a modification to go to the next space first. I didn't compile this, but it should be close.

public static String limit(String value, int length)
  {
    // note to Test this first, so you don't create a Buffer unnecessarily.
    String ret = value;
    if ( value.length() > length ) {
       StringBuilder buf = new StringBuilder(value);
       buf.setLength( length );
       int cur = length;
       while( cur < value.length() && value.charAt(cur) != ' ' ) {
           buf.append( value.charAt(cur) );
       }
       if ( cur == value.length() ) {
          ret = value;  // we copied the whole string, as it turns out.
       } else {
          buf.append( "..." );
          ret = buf.toString();
       }
    }
    return value;
  }
Sign up to request clarification or add additional context in comments.

5 Comments

Also note that it appends the elipses AFTER the limiting, so you'll actually get a String back that's 53 characters long.
You were right, I needed to make a value out of the new result. Marking correct.... thanks again.
Bonus Question: I am truncating sentence but don't want to cut of words, can I add something where it only stops at the next space?
You're opening a can of worms there. Do you want the NEXT space after limit, or maybe the one before limit? What if doing the first makes a String 60+ chars long? Or the 2nd makes one < 40? Is there a reason you're writing a word-wrapper, when packages exist out there?
Upon reconsideration, I think I'll just take the original answer and go with that!
0
public static String limit(String value, int length)
  {
    return value+"...";
  }

1 Comment

The code is already fine as it is. The problem is another one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.