Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

replace values using ascci table

Posted on 2014-10-31
3
Medium Priority
?
119 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 35

Accepted Solution

by:
it_saige earned 1400 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 100 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Integration Management Part 2
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

971 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