-2

I have a string like this

String myString = "I love [stackoverflow]";

What I want to do is to replace the character "[" to <span style="color:red;"> and the character "]" to </span> to change the color of the word stackoverflow to red.

So the expected string result is "I love <span style="color:red;">stackoverflow</span>"

I am new in regex so I tried like this

String myString = "I love [Stackoverflow]";

myString.replaceAll("[",<span style="color:red;">).replaceAll("]","</span>");

but seems not the correct way because the android studio says

unclosed character class

What is the correct regex of that single character?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Noryn Basaya
  • 664
  • 1
  • 5
  • 21

1 Answers1

0
String myString = "I love [stackoverflow]";

    myString = myString.replace("[", "<b><font color=\"#FF0000\">").replace("]", "</font>");

    etEmail = (EditText) findViewById(R.id.et_email);
    etEmail.setText(Html.fromHtml(myString));

PLease check attached image...

Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37