Solved

replace values using ascci table

Posted on 2014-10-31
3
108 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 35

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 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 37
Resolve Dependency Issues 4 47
scanning dentists xray (the small ones) 3 45
ASP.NET 5 Templates 2 66
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now