Solved

replace values using ascci table

Posted on 2014-10-31
3
106 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 32

Accepted Solution

by:
it_saige earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

10 Experts available now in Live!

Get 1:1 Help Now