diff options
Diffstat (limited to 'core/java/android/text/TextUtils.java')
| -rw-r--r-- | core/java/android/text/TextUtils.java | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java index 6b2f80241df3..dde4c1d2801f 100644 --- a/core/java/android/text/TextUtils.java +++ b/core/java/android/text/TextUtils.java @@ -2091,6 +2091,25 @@ public class TextUtils { return (T) text.subSequence(0, size); } + /** + * Trims the {@code text} to the first {@code size} characters and adds an ellipsis if the + * resulting string is shorter than the input. This will result in an output string which is + * longer than {@code size} for most inputs. + * + * @param size length of the result, should be greater than 0 + * + * @hide + */ + @Nullable + public static <T extends CharSequence> T trimToLengthWithEllipsis(@Nullable T text, + @IntRange(from = 1) int size) { + T trimmed = trimToSize(text, size); + if (trimmed.length() < text.length()) { + trimmed = (T) (trimmed.toString() + "..."); + } + return trimmed; + } + private static Object sLock = new Object(); private static char[] sTemp = null; |
