Solved

replace values using ascci table

Posted on 2014-10-31
3
113 Views
Last Modified: 2014-11-05
hi,

My code uses the String.Replace method to replace various characters that the browser cannot interpret due to encoding

Example:
group.Name.Replace("½", "½").Replace("¼", "¼").Replace("¾", "¾").Replace("°", "°").Replace("Â", "")

I know it does not look pretty, and that is why I would like to ask if it would be possible to use a subroutine in an utility class, with an array containing all ascci values from a table like in this page: http://www.econlib.org/library/asciicodes.html, and then call it from anywhere in my program.

I am not verse in coding something like this, but with a bit of help and guidance I can dare to give it a try.

If anyone could give me a hand with starting the code, if not asking to much, that would be great.
Or at least some direction on how to best approach this issue.

Thank you kindly.
0
Comment
Question by:metropia
3 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 350 total points
ID: 40416597
It really wouldn't be that difficult to use an extension method to do this, you would just use a switch statement to replace the found character with the encoded string.

Something like:
Module Extensions
	<System.Runtime.CompilerServices.Extension()> _
	Public Function ToHTMLEncoded(ByVal StringIn As String) As String
		Dim result As String = String.Empty
		For Each character As Char In StringIn
			Select Case character
				Case "¼"
					result = String.Format("{0}&#188;", result)
				Case "½"
					result = String.Format("{0}&#189;", result)
				Case "¾"
					result = String.Format("{0}&#190;", result)
				Case "—"
					result = String.Format("{0}&#151;", result)
				Case Else
					result = String.Format("{0}{1}", result, character)
					Exit Select
			End Select
		Next
		Return result
	End Function
End Module

Public Module Module1
	Public Sub Main()
		Dim teststring As String = "I had a brown cow that was ¼ blind and ½ deaf."
		Console.WriteLine(teststring.ToHTMLEncoded)
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:Capture.JPG
This should get you started.

-saige-
0
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 25 total points
ID: 40417630
Most of the conversion should be done by HtmlEncode method from System.Net namespace, but given the fact that there is one letter(Â) that is not encoded(but set the empty string), you code needs to be modified as follows:
group.Name = WebUtility.HtmlEncode(group.Name.Replace("Â", "")) 

Open in new window

0
 
LVL 28

Assisted Solution

by:Ark
Ark earned 125 total points
ID: 40417828
Public Function StringToHTML(ByVal input As String) As String
    Return String.Join("", input.ToCharArray.Select(Function(c) CharToHtml(c)).ToArray)
End Function
Private Function CharToHtml(ByVal c As Char) As String
    Dim val = Convert.ToInt32(c)
    Dim ret As String
    If val > 127 Then 
        'Add more check here if need (for example for "Â")
        'If val>190 Then Return ""
        ret = "&#" & val & ";"
    Else
        ret = c.ToString
    End If
    Return ret
End Function

Open in new window

Using: MsgBox(StringToHTML("string to ½ html ¼test¾"))
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question