Replacing characters in WEB text

  • Thread starter Thread starter expertware
  • Start date Start date
E

expertware

Dear friend as a routine to replace special character when one has to
output text on a web page (client side) I have used this one as a quick
and dirty way:

---------------------------------------------------------------------

Dim WebSymbols As String() = New String() { _
"&", "&", _
">", ">", _
"<", "&lt;", _
(...Long list omitted...)
"û", "&ucirc;", _
"ü", "&uuml;", _
"ý", "&yacute;", _
"þ", "&thorn;", _
"ÿ", "&yuml;", _
" ", "&nbsp;", _
vbCrLf, "<br>" _
}

'Note: &amp; must precede all, <br> must follow &gt; &lt;

Public Function FixStringForWEB(ByVal Text As String) As String

Dim WebText As New System.Text.StringBuilder(Text)
For i As Integer = 0 To WebSymbols.Length - 1 Step 2
WebText = WebText.Replace(WebSymbols(i), WebSymbols(i + 1))
Next i
Return WebText.ToString

End Function

-------------------------------------------------------------

I feel this method is unsatisfactory and ackward. I am wondering
if anyone can suggest a method more elegant and especially faster
(although the above is not that slow).

-Pamela
 
Depending on what you are doing, both the HttpUltility class and
HttpServerUtility class (both in System.Web) have HtmlEncode methods, which
will do what you desire.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
This sure sounds like a job for System.Web.HttpUtility.HtmlEncode(string) to
me.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.


Dear friend as a routine to replace special character when one has to
output text on a web page (client side) I have used this one as a quick
and dirty way:

---------------------------------------------------------------------

Dim WebSymbols As String() = New String() { _
"&", "&amp;", _
">", "&gt;", _
"<", "&lt;", _
(...Long list omitted...)
"û", "&ucirc;", _
"ü", "&uuml;", _
"ý", "&yacute;", _
"þ", "&thorn;", _
"ÿ", "&yuml;", _
" ", "&nbsp;", _
vbCrLf, "<br>" _
}

'Note: &amp; must precede all, <br> must follow &gt; &lt;

Public Function FixStringForWEB(ByVal Text As String) As String

Dim WebText As New System.Text.StringBuilder(Text)
For i As Integer = 0 To WebSymbols.Length - 1 Step 2
WebText = WebText.Replace(WebSymbols(i), WebSymbols(i + 1))
Next i
Return WebText.ToString

End Function

-------------------------------------------------------------

I feel this method is unsatisfactory and ackward. I am wondering
if anyone can suggest a method more elegant and especially faster
(although the above is not that slow).

-Pamela
 
Dear Gregory, Dear Kevin,

thank you very much for the useful pointer!!

I was pretty sure I was missing something, as this is a task too common
for
not already being in the framework.
I will go read the documentation.
For now do you confirm that System.Web.HttpUtility.HtmlEncode(string)
does the job of replacing the special chars with appropriate html as I
was
doing with the FixStringForWEB() function?

Thank you!!
-Pamela

Kevin Spencer ha scritto:
 
I can see it is used mostly in web application.

I need it for a Windows application and at the moment it is not very
clear to me how to use it.
Do I have to instantiate some object or add includes?
(if you have any snippet showing its use in win apps would be great)

-Pamela
 
Dear Gregory, Dear Kevin,

thank you very much for the useful pointer!!

I was pretty sure I was missing something, as this is a task too
common for
not already being in the framework.
I will go read the documentation.
For now do you confirm that System.Web.HttpUtility.HtmlEncode(string)
does the job of replacing the special chars with appropriate html as I
was
doing with the FixStringForWEB() function?

You can write a simple test to prove that yourself. Note that
HttpUtility.HtmlEncode() isn't perfect though. It does not encode all
characters it should encode, nor does it consistently use either
character or numeric references.

On the other hand, if you know how to properly encode these characters
in HTML natively, you don't really need HTML encoding save for some
special cases like "&amp;".

Cheers,
 
Dear Joerg and other readers,

I have a couple of question:
- If I understand well you are suggesting it is better I continue using
my own function, to be sure all chars
are encoded correcly. Right ?

- another one regards the FixStringForWEB() function itself
Do you think it it more convenient to scan the string once
replacing the chars, or the approach or iterate replace
using stringbuilder is more appropriate ?

Thank you!

-Pamela
 
Dear Joerg and other readers,

I have a couple of question:
- If I understand well you are suggesting it is better I continue
using my own function, to be sure all chars
are encoded correcly. Right ?

Probably. As I said, write a little test and see if HtmlEncode()
encodes all characters you need. If it does, use it.
- another one regards the FixStringForWEB() function itself
Do you think it it more convenient to scan the string once
replacing the chars, or the approach or iterate replace
using stringbuilder is more appropriate ?

I'd still say you don't need that method at all. You can use all these
characters natively (without HML encoding) in your C#, HTML, or XHTML
source files. By default, ASP.NET uses UTF-8 as request and response
encoding, which is good enough for your purposes. The only thing you
might need to change is fileEncoding in web.config.

Cheers,
 
I'd still say you don't need that method at all. You can use all these
characters natively (without HML encoding) in your C#, HTML, or XHTML
source files. By default, ASP.NET uses UTF-8 as request and response
encoding, which is good enough for your purposes. The only thing you
might need to change is fileEncoding in web.config.

Thank you Joerg,

I think you are implicitly assuming I am making a web application.
However my point of view is completely different. As I think I have
said, I am generating an html file from a windows application. Let's
say an export utility which generates an html file. So no web server,
no asp.

If I understood well all your advice is referring to web application.
I was wondering if the html encoding is usable also in my case.

-Pamela
 
Back
Top