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?
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
Ok, you asked for a modification to go to the next space first. I didn’t compile this, but it should be close.