Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

replace values using ascci table

Posted on 2014-10-31
3
Medium Priority
?
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

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

Industry Leaders: 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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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