MentionsEditable extends SpannableStringBuilder implements Parcelable { @NonNull public MentionsEditable trim() { while (length() > 0 && Character.isWhitespace(charAt(0))) { delete(0, 1); } int len = length(); while (len > 0 && Character.isWhitespace(charAt(len - 1))) { delete(len - 1, len); len--; } return new MentionsEditable(this, 0, length()); } MentionsEditable(@NonNull CharSequence text);  MentionsEditable(@NonNull CharSequence text, int start, int end);  MentionsEditable(@NonNull Parcel in); @Override void setSpan(Object what, int start, int end, int flags); @NonNull @Override SpannableStringBuilder replace(int start, int end, CharSequence tb, int tbstart, int tbend); @NonNull MentionsEditable trim(); @NonNull List<MentionSpan> getMentionSpans(); @Nullable MentionSpan getMentionSpanAtOffset(int index); @Nullable MentionSpan getMentionSpanStartingAt(int index); @Nullable MentionSpan getMentionSpanEndingAt(int index); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); static final Parcelable.Creator<MentionsEditable> CREATOR; }
@Test public void testTrim() { mEditable = new MentionsEditable(" " + NAME + " "); mEditable.setSpan(mMentionSpan, 2, 2 + NAME.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mEditable = mEditable.trim(); assertEquals(NAME, mEditable.toString()); assertEquals(0, mEditable.getSpanStart(mMentionSpan)); assertEquals(mEditable.length(), mEditable.getSpanEnd(mMentionSpan)); mEditable = new MentionsEditable("\n\t" + NAME); mEditable.setSpan(mMentionSpan, 2, 2 + NAME.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mEditable = mEditable.trim(); assertEquals(NAME, mEditable.toString()); assertEquals(0, mEditable.getSpanStart(mMentionSpan)); assertEquals(mEditable.length(), mEditable.getSpanEnd(mMentionSpan)); }