Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// HTML-encodes a string and returns the encoded string.
- /// </summary>
- /// <param name="text">The text string to encode. </param>
- /// <returns>The HTML-encoded text.</returns>
- public static string HtmlEncode(string text) {
- if(text == null)
- return null;
- StringBuilder sb = new StringBuilder(text.Length);
- int len = text.Length;
- for(int i = 0;i < len;i++) {
- switch(text[i]) {
- case '<':
- sb.Append("<");
- break;
- case '>':
- sb.Append(">");
- break;
- case '"':
- sb.Append(""");
- break;
- case '&':
- sb.Append("&");
- break;
- default:
- if(text[i] > 159) {
- // decimal numeric entity
- sb.Append("&#");
- sb.Append(((int)text[i]).ToString(CultureInfo.InvariantCulture));
- sb.Append(";");
- }
- else
- sb.Append(text[i]);
- break;
- }
- }
- return sb.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment