public SpannableStringBuilder replace(final int start, final int end,
            CharSequence tb, int tbstart, int tbend) {
        checkRange("replace", start, end);

        int filtercount = mFilters.length;
        for (int i = 0; i < filtercount; i++) {
            CharSequence repl = mFilters[i].filter(tb, tbstart, tbend, this, start, end);

            if (repl != null) {
                tb = repl;
                tbstart = 0;
                tbend = repl.length();
            }
        }

        final int origLen = end - start;
        final int newLen = tbend - tbstart;

        if (origLen == 0 && newLen == 0 && !hasNonExclusiveExclusiveSpanAt(tb, tbstart)) {
            // This is a no-op iif there are no spans in tb that would be added (with a 0-length)
            // Early exit so that the text watchers do not get notified
            return this;
        }

        TextWatcher[] textWatchers = getSpans(start, start + origLen, TextWatcher.class);
        sendBeforeTextChanged(textWatchers, start, origLen, newLen);

        // Try to keep the cursor / selection at the same relative position during
        // a text replacement. If replaced or replacement text length is zero, this
        // is already taken care of.
        boolean adjustSelection = origLen != 0 && newLen != 0;
        int selectionStart = 0;
        int selectionEnd = 0;
        if (adjustSelection) {
            selectionStart = Selection.getSelectionStart(this);
            selectionEnd = Selection.getSelectionEnd(this);
        }

        change(start, end, tb, tbstart, tbend);

        if (adjustSelection) {
            if (selectionStart > start && selectionStart < end) {
                final int offset = (selectionStart - start) * newLen / origLen;
                selectionStart = start + offset;

                setSpan(false, Selection.SELECTION_START, selectionStart, selectionStart,
                        Spanned.SPAN_POINT_POINT);
            }
            if (selectionEnd > start && selectionEnd < end) {
                final int offset = (selectionEnd - start) * newLen / origLen;
                selectionEnd = start + offset;

                setSpan(false, Selection.SELECTION_END, selectionEnd, selectionEnd,
                        Spanned.SPAN_POINT_POINT);
            }
        }

        sendTextChanged(textWatchers, start, origLen, newLen);
        sendAfterTextChanged(textWatchers);

        // Span watchers need to be called after text watchers, which may update the layout
        sendToSpanWatchers(start, end, newLen - origLen);

        return this;
    }
public SpannableStringBuilder replace ( final int start , final int end , CharSequence tb , int tbstart , int tbend ) { checkRange ( "replace" , start , end ) ; int filtercount = mFilters . length ; for ( int i = 0 ; i < filtercount ; i ++ ) { CharSequence repl = mFilters [ i ] . filter ( tb , tbstart , tbend , this , start , end ) ; if ( repl != null ) { tb = repl ; tbstart = 0 ; tbend = repl . length ( ) ; } } final int origLen = end - start ; final int newLen = tbend - tbstart ; if ( origLen == 0 && newLen == 0 && ! hasNonExclusiveExclusiveSpanAt ( tb , tbstart ) ) { return this ; } TextWatcher [ ] textWatchers = getSpans ( start , start + origLen , TextWatcher . class ) ; sendBeforeTextChanged ( textWatchers , start , origLen , newLen ) ; boolean adjustSelection = origLen != 0 && newLen != 0 ; int selectionStart = 0 ; int selectionEnd = 0 ; if ( adjustSelection ) { selectionStart = Selection . getSelectionStart ( this ) ; selectionEnd = Selection . getSelectionEnd ( this ) ; } change ( start , end , tb , tbstart , tbend ) ; if ( adjustSelection ) { if ( selectionStart > start && selectionStart < end ) { final int offset = ( selectionStart - start ) * newLen / origLen ; selectionStart = start + offset ; setSpan ( false , Selection . SELECTION_START , selectionStart , selectionStart , Spanned . SPAN_POINT_POINT ) ; } if ( selectionEnd > start && selectionEnd < end ) { final int offset = ( selectionEnd - start ) * newLen / origLen ; selectionEnd = start + offset ; setSpan ( false , Selection . SELECTION_END , selectionEnd , selectionEnd , Spanned . SPAN_POINT_POINT ) ; } } sendTextChanged ( textWatchers , start , origLen , newLen ) ; sendAfterTextChanged ( textWatchers ) ; sendToSpanWatchers ( start , end , newLen - origLen ) ; return this ; }
Documentation from interface