Java tutorial
//package com.java2s; /* * Licensed Materials - Property of IBM * Copyright IBM Corporation 2015. All Rights Reserved. */ import android.text.SpannableString; import android.text.Spanned; import android.text.style.StyleSpan; public class Main { /** * Generate a SpannableString that styles a specific sub-sequence of a CharSequence object. * * @param sequence The original text. * @param subSequence A sub-sequence of the original text. * @param styleSpan The style to be applied to the sub-sequence. * @return A SpannableString with the style applied to only the sub-sequence of the original * text. */ public static SpannableString spanSubstring(CharSequence sequence, CharSequence subSequence, StyleSpan styleSpan) { SpannableString spannableString = new SpannableString(sequence); int startIndex = spannableString.toString().indexOf(subSequence.toString()); int endIndex = startIndex + subSequence.toString().length(); spannableString.setSpan(styleSpan, startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); return spannableString; } }