I have been making more use of CharSequence in text processing to avoid unnecessary generating Strings.
A trivial example is.
public CharSequence cut(CharSequence str, int len) {
if (str.length <= len)
return;
StringBuilder ret = new StringBuilder(len);
ret.append(str.subSequence(0, len-3));
ret.append("...");
return ret;
}
This result can be used in a StringBuilder, or be passed to a method which requires a String.
e.g.
public void setDescription(String description) { ... }
setDescription(cut(longDescription, MAX_LENGTH));
One of the options is to cast to String.
setDescription((String) cut(longDescription, MAX_LENGTH));
This may work some of the time, but a better, possiblily more reliable suggestion is call toString()
setDescription(cut(longDescription, MAX_LENGTH).toString());
I believe calling toString() may be a better way convert in a wide range of situations.