105

I'm trying to write a blog post which includes a code segment inside a <pre> tag. The code segment includes a generic type and uses <> to define that type. This is what the segment looks like:

<pre>
    PrimeCalc calc = new PrimeCalc();
    Func<int, int> del = calc.GetNextPrime;
</pre>

The resulting HTML removes the <> and ends up like this:

PrimeCalc calc = new PrimeCalc();
Func del = calc.GetNextPrime;

How do I escape the <> so they show up in the HTML?

3
  • 4
    &lt; and &gt;
    – Alan Dong
    Commented May 4, 2017 at 3:57
  • 2
    You could just use an online formatter: freeformatter.com/html-escape.html
    – kaore
    Commented Mar 26, 2018 at 7:24
  • the term is "html encoding", AKA "html escaping". In ASP.NET, you can do it like this: System.Net.WebUtility.HtmlEncode("<h1>Your HTML here</h1>")
    – Emre Bener
    Commented Nov 14, 2023 at 14:48

8 Answers 8

125
<pre>
    PrimeCalc calc = new PrimeCalc();
    Func&lt;int, int&gt; del = calc.GetNextPrime;
</pre>
3
  • 20
    I feel bad that there is no technology still introduced for this. Commented Jul 13, 2014 at 5:26
  • 1
    Of course, the ampersand is also something that needs to be quoted in the same context as angular brackets, so am not so sure how useful it is to use '&' if '<' and '>' need html entities.
    – Astara
    Commented Mar 28, 2017 at 21:29
  • @EmreBener it did exist; the element was introduced in HTML 2 and became standard in HTML 3.2 (1997). martinrinehart.com/frontend-engineering/engineers/html/…
    – qwr
    Commented Nov 10, 2023 at 17:21
24
<pre>&gt;</pre>

renders as:

>

So you want:

<pre>
    PrimeCalc calc = new PrimeCalc();
    Func&lt;int, int&gt; del = calc.GetNextPrime;
</pre>

which turns out like:

    PrimeCalc calc = new PrimeCalc();
    Func<int, int> del = calc.GetNextPrime;
18

Use &lt; and &gt; to do < and > inside html.

8

&lt; and &gt; respectively

7

How about:

&lt; and &gt;

Hope this helps?

3

What rp said, just replace the greater-than(>) and less-than(<) symbols with their html entity equivalent. Here's an example:

<pre>
    PrimeCalc calc = new PrimeCalc();
    Func&lt;int, int&gt; del = calc.GetNextPrime;
</pre>

This should appear as (this time using exactly the same without the prepended spaces for markdown):

    PrimeCalc calc = new PrimeCalc();
    Func<int, int> del = calc.GetNextPrime;
-1

It's probably something specific to your blog software, but you might want to give the following strings a try (remove the underscore character): &_lt; &_gt;

-6

A better way to do is not to have to worry about the character codes at all. Just wrap all your code inside the <pre> tags with the following

<pre>
${fn:escapeXml('
  <!-- all your code -->
')};
</pre>

You'll need to have jQuery enabled for it to work, tho.

1
  • 3
    This is not better. You would then need to escape your content for JavaScript. In some cases, it is still possible to break out of the JavaScript. For instance, what if the content had </pre> in it?
    – Brad
    Commented Jan 13, 2015 at 22:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.