- Home /
decode html characters in c#
Anyone know how to decode html character codes such as ( & # x 0 0 4 0; ) in C# in Unity?
All examples I can find are using libraries that arent included.
Or maybe an idea on which library to include and how ;)
Answer by Bunny83 · Apr 26, 2012 at 11:13 AM
Google is your friend:
http://stackoverflow.com/questions/1631819/htmlencode-in-c-sharp
Anyway, if you're just interested in the unicode escape sequences you can convert them yourself.
Search for "&#x" in the srting
extract the numbers until ";"
convert the hexnumber string to an int
cast the int to char and you get your character
I would do something like this:
string DecodeHtmlChars(string aText)
{
string[] parts = aText.Split(new string[]{"&#x"}, StringSplitOptions.None);
for (int i = 1; i < parts.Length; i++)
{
int n = parts[i].IndexOf(';');
string number = parts[i].Substring(0,n);
try
{
int unicode = Convert.ToInt32(number,16)
parts[i] = ((char)unicode) + parts[i].Substring(n+1);
} catch {}
}
return String.Join("",parts);
}
Haven't tested it yet, but should work ;)
edit Just remembered that the number is a hexadecimal string. Changed the code...
Note that there are also other escape sequences like "`&`". There's also the decimal unicode format "`@`" == "`@`"
After fixing a few "writing errors" (Length with capitol L and space between new string[] and {"&#x"} it works just fine. Thank you so much!
Here's my string extension class I made from your example:
using UnityEngine;
using System; using System.Collections;
public static class StringExtensions {
public static string DecodeHtmlChars(this string source)
{
string[] parts = source.Split(new string[] {"&#x"}, StringSplitOptions.None);
for (int i = 1; i < parts.Length; i++)
{
int n = parts[i].IndexOf(';');
string number = parts[i].Substring(0,n);
try
{
int unicode = Convert.ToInt32(number,16);
parts[i] = ((char)unicode) + parts[i].Substring(n+1);
} catch {}
}
return String.Join("",parts);
}
}
@Breakmachine: Yes, thanks for the hints. Recently i've done a lot LUA program$$anonymous$$g so i'm a bit rusty with .NET / C# and i've written this from scratch ;)
btw. you don't need a space between the array type and the initializer brackets, but i forgot the StringSplitOptions. I've fixed my code.
If you want to handle all variants you could split at "&", copy the string until ";" and then check for the 3 different methods: hexstring (starts with "x"), decimal number, one of those keywords
Answer by Miraculade · Aug 22, 2016 at 07:22 PM
Someone grabbed the Mono-HttpUtility to use it in Unity: https://github.com/Cratesmith/RestSharp-for-unity3d/tree/master/RestSharp/Extensions/MonoHttp
Import all three classes into your project, and use the RestSharp.Contrib
namespace to call HttpUtility.HtmlDecode (yourHtmlTextToDecode);
Answer by devilkkw · Apr 26, 2012 at 10:36 AM
string aaa = myText.Text;//text to decode.
string b = aaa.Replace("&", "WAT YOU WANT");
myText.Text=b;
just use it on a void called by a button click. We haven't more datails for give best help: have u to replace stirung in a label,in a butto,or in other?
Well, he don't want to just replace a string. It's about the unicode html encoding format. The sequence "@" is the character "@" for example.
Your answer
Follow this Question
Related Questions
Please help me about this problem 0 Answers
How to load html content 2 Answers
Need to add sub-total type running total to a form with radio buttons 0 Answers
Unable to connect to Update Server 1 Answer
how to use Iframe 1 Answer