meee meeee
meee meeee

Reputation: 211

error replaceAll() java method

I have these tags in my textarea

<gras>.....</gras>

And I'm trying to replace them using the replaceAll() String method

text.replaceAll("<gras>", "<b>");

text.replaceAll("</gras>", "</b>");

But, this regex code doesn't work. Any help please ?

Upvotes: 0

Views: 168

Answers (2)

JNL
JNL

Reputation: 4713

You forgot a very important concept;

. Change text.replaceAll("<gras>", "Bold!");

To

text = text.replaceAll("<gras>", "Bold!");

Assign text = some Function, as text.replace() is creating a new String object and not referencing it.

Hope this helps.

Upvotes: 5

Edwin Buck
Edwin Buck

Reputation: 70939

Strings don't replace. Strings construct new Strings with the replacement values.

Also, if you are dealing with XML, regex is the wrong tool. That doesn't mean it can't work, and it might be useful in some limited examples, but it shouldn't be the first tool to use. Much like a hammer shouldn't be the first tool to use when installing a screw.

Upvotes: 2

Related Questions