0

Actually, I wanted to HTMLDecode a text using HttpUtility.HtmlDecode() function and it works properly with whole text, but I want to decode the text except some specific string. How can I do that?

Specifically, I don't want to decode < and >

Let's say my string is - &lt; &amp; &gt;

HttpUtility.HtmlDecode(text) returns - < & >

Wanted Output - &lt; & &gt;

2
  • You can use Replace method for this, my friend :)) Commented Dec 3, 2018 at 4:53
  • @Tomato32 The problem in the replace method is if the text has HTML tags <> then it will replace that as well. So, trying to figure out any better compact solution for this. Thanks for your reply. Commented Dec 3, 2018 at 5:30

1 Answer 1

0

You could replace the strings you don't want to change to something the decoder will not recognize then bring them back to the original value. You need to make sure that the string you replace to is not something that can be found in the string normally.

Public Function Convert1(ByVal str As String) As String

    str = str.Replace("&lt;", "{lt}")
    str = str.Replace("&gt;", "{gt}")

    str = HttpUtility.HtmlDecode(str)

    str = str.Replace("{lt}", "&lt;")
    str = str.Replace("{gt}", "&gt;")

    Return str
End Function

An other option would be to split of your string and Decode only the part you want.

Public Function Convert2(ByVal str As String) As String

    Dim parts() As String = Regex.Split(str, "(&lt;|&gt;)")

    For i As Integer = 0 To parts.Length - 1
        Console.WriteLine(parts(i))
        If parts(i) <> "&lt;" And parts(i) <> "&gt;" Then
            parts(i) = HttpUtility.HtmlDecode(parts(i))
        End If
    Next

    Return String.Join("", parts)
End Function

This is just a sample, some check and refactoring would need to be done.

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.