0

I have this string:

String s="<div style='font: 12px arial, sans-serif;'><b>Menu:<<>></b></div>"

I want to change the <<>> to &amp;lt;&amp;lt;&amp;gt;&amp;gt; to get the output:

String s="<div style='font: 12px arial, sans-serif;'><b>Menu:&amp;lt;&amp;lt;&amp;gt;&amp;gt;</b></div>"

I have used regex once a while back to retrieve text from the tags but how is this possible? I need the correct output.

I aim for this to be for general-purpose HTML tags, not just the particular ones in this post.

7
  • 1
    This looks like a job for using regex to parse html! Commented Nov 20, 2014 at 8:48
  • @T.J.Crowder Java .. i have rectified the post .. its just to give an idea not the actual code .. Commented Nov 20, 2014 at 8:49
  • Why do you want to turn <<>> into &amp;lt;&amp;lt;&amp;gt;&amp;gt; (which would actually render as &lt;&lt;) rather than &lt;&lt;&gt;&gt;?! Commented Nov 20, 2014 at 8:49
  • @T.J.Crowder its a requirement for other modules .. Commented Nov 20, 2014 at 8:50
  • @Rafay: I'd question it, it doesn't seem to make any sense. You'll end up with HTML that shows the character entities, rather than characters: jsbin.com/vabede/1 Commented Nov 20, 2014 at 8:52

1 Answer 1

1

you can use commons-lang's StringEscapeUtils class to do this.

string s="<div style='font: 12px arial, sans-serif;'><b>Menu:<<>></b></div>";    
System.out.println(StringEscapeUtils.escapeHtml(s));

PS: your questions like this:Is there a quick way to recognize HTML ASCII codes in a String or TextView?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.