Smart little helper to clean HTML based strings for [WPDEV] and [WIN8DEV]
I thought I would share this small little class I created to clean out HTML based strings into readable strings. The class is both usable for your Windows Phone app as well as your Windows 8 app.
If you use cloud based or internet data, often strings contain “–” or “–” instead of their intended letter, like you can see on this image:
The method uses Regex to replace all the wrong signs and clean out all of the unwanted signs. You can also add more to that if you need to replace wrong letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class CleanHTML
{
public static string RemoveEncoding(string text)
{
try
{
string temp="";
temp =
Regex.Replace
(text.
Replace("–", "-").
Replace(" ", " ").
Replace("’", "'").
Replace("&", "&").
Replace("&", "&").
Replace(""", """).
Replace("'", "'").
Replace("…", "...").
Replace("—", "—").
Replace("–", "-").
Replace("“", "“").
Replace("”", "”").
Replace("’", "'").
Replace(" ", " ").
Replace(">", ">").
Replace("”", """).
Replace("“", """).
Replace("<", "<").
Replace("×", "×").
Replace("′", "′").
Replace("″", "″").
Replace("‘", "'"),
"<[^<>]+>", "");
return temp;
}
catch
{
return "";
}
}
}
As we created a class for this little helper, you can call it from everywhere within you app to clean out the string. Here is one example I am using:
1
item.title = CleanHTML.RemoveEncoding(item.title);
After calling this method, your string is plain text:
I hope this post will be helpful for some of you.
Happy coding!
Comments powered by Disqus.