1

Here Example : "some [string] with [the data i want] inside [ashish]"

i want to get string with blue color that are between "["blue colored"]" and replace [ (opening) (closing)] to just space. like facebook tagging statement.

bytecode77
  • 13,209
  • 30
  • 105
  • 134
  • you just want to replace "[" and "]" characters with space? – chandil03 Jul 28 '15 at 11:04
  • that is easy .. i want replacement as well as get string between "[" and "]" with blue color. so i my example i want some "blue part " with "blue part " inside "blue part – user3871622 Jul 28 '15 at 11:10

1 Answers1

0

Try following method:

    private SpannableString makeBluish(String text)
{
    String temp = text;
    Pattern pattern = Pattern.compile("\\[([\\sa-zA-Z]+)\\]");
    Matcher matcher = pattern.matcher(text);
    while (matcher.find())
    {
        temp = temp.replace(matcher.group(0), (matcher.group(1)));
    }

    SpannableString linkedString = new SpannableString(temp);
    matcher = pattern.matcher(text);
    while (matcher.find())
    {

        final String token = matcher.group(1);

        int index = temp.indexOf(token);

        linkedString.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + token.length(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
    }
    return linkedString;
}

And

textString.setText(makeBluish("some [string] with [the data i want] inside [ashish]"));

here textString is a TextView.

chandil03
  • 15,885
  • 7
  • 45
  • 65
  • Thnq for answer . this is not working for me. so i get my answer by http://notepad.cc/bluis – user3871622 Jul 28 '15 at 12:55
  • This is a working code, I have checked it. may be some issue on implementation side. You have solved you problem so doesn't matter, leave it. – chandil03 Jul 28 '15 at 12:58