Geeks With Blogs

Tim Huffam Dotting the I and crossing the T of I.T.

 

public string HTMLEncodeSpecialChars(string text)
{
  System.Text.StringBuilder sb = new System.Text.StringBuilder();
  foreach (char c in text){
    if(c>127) // special chars
      sb.Append(String.Format("&#{0};",(int)c));
    else
      sb.Append(c);
  }
  return sb.ToString();
}

Posted on Thursday, May 25, 2006 12:31 PM C# .NET , ASP.NET , DotNetNuke (DNN) | Back to top


Comments on this post: How to convert special characters to HTML encoded equivalents using C#.NET

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Excellent!
Just what I was looking for...

Best regards
Lars B.
Left by Lars B. on Jun 01, 2006 6:53 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Many thanks
Left by Ian Stone on Dec 20, 2006 3:14 PM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Thanks for your contribution!
Left by Wei on Jul 12, 2007 9:10 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Thanks alot,

You saved my day :)
Left by Serkan on Dec 27, 2007 3:34 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
How can i convert "&#{0};" to understandable charaters in html?
Left by ptp on Mar 07, 2008 12:13 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
??

Ok, I had a HTML encode, but How Can I convert the Ascii Code?
Left by fernando on Mar 14, 2008 9:45 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Many thanks, this has saved tons of my time.
Left by Captain Jack on Jun 11, 2008 5:10 PM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
html special characters --
http://html-lesson.blogspot.com/2008/06/special-characters.html
Left by javascript on Jun 16, 2008 10:56 PM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
HI i need your help i really want to create my own website/web page but i dont know how to go about doing it so can you please help me out
Left by Css Lessons on Jul 06, 2008 3:00 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Thanks a lot. It was really very useful.
Left by Neel on Aug 22, 2008 10:19 PM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Why not use Server.HTMLEncode instead? Easier than doing a loop.

See: http://www.w3schools.com/ASP/met_htmlencode.asp
Left by Anthony on Dec 03, 2008 12:32 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Great! What I was looking for!
Just Added "less than" and "more than"
public string HTMLEncodeSpecialChars(string text)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (char c in text)
{
if (c > 127 || c == 60 || c == 62) // special chars
sb.Append(String.Format("&#{0};", (int)c));
else
sb.Append(c);
}
return sb.ToString();
}
Left by Dan on Apr 14, 2009 8:48 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
public static string HTMLEncodeSpecialChars(string text)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (text != null)
foreach (char c in text)
{
if (!Char.IsLetterOrDigit(c)) // special chars
sb.Append(String.Format("&#{0};", (int)c));
else
sb.Append(c);
}
return sb.ToString();
}
Left by Casto Colina on Feb 27, 2010 8:46 PM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Many thanks for this, very handy indeed!
Left by Andy on Mar 02, 2010 5:31 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
string = Server.HtmlEncode(string),
Left by alvaro on Apr 07, 2010 6:30 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Thanks alot! You have helped me too :)
Left by Spara on Aug 13, 2010 2:44 AM

# re: sir mai hindi me jab ' type karta hoo to yeh data base me save nahi hota
Requesting Gravatar...
hindi me jab ' type karta hoo to yeh data base me save nahi hota...
Left by rahul on Oct 07, 2010 3:33 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
In case someone needs a reverse function, here is a sample:
string encodedString = "Аја";
string[] splittedChars;
string decodedString = "";
splittedChars= endodedString.Split(new char[] { ';' });
foreach (string s in splittedChars) {
decodedString+= (char)(Convert.ToInt32(s.Replace("&#", "")));
}
Left by M on Nov 19, 2010 6:20 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Great work.... thanks
Left by Learner on Jan 20, 2011 1:10 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Either that, or just use HttpUtility.HtmlEncode() and HttpUtility.HtmlDecode().
Left by Jonathan Wood on Apr 05, 2011 11:58 AM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
This worked like a charm. Thanks a lot for sharing this code.

Thanks again,

Krunal
Left by Krunal on Sep 06, 2012 3:04 PM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
Had issues w/ the > 127 version, but the !Char.IsLetterOrDigit worked flawlessly!

many thanks!

-noobie
Left by noobie on Sep 24, 2012 1:29 PM

# re: How to convert special characters to HTML encoded equivalents using C#.NET
Requesting Gravatar...
It works perfectly, thanks again.
Left by Conservatory Furniture on Feb 11, 2013 6:22 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net