### Escape HTML and URL characters

The subs below are examples of escaping characters for HTML and URL (for using mailto:).
`Public Sub EscapeHtml(Raw As String) As String
Dim sb As StringBuilder
sb.Initialize
For i = 0 To Raw.Length - 1
Dim C As Char = Raw.CharAt(i)
Select C
Case CRLF
sb.Append("
")
Case Chr(9)
sb.Append(" ")
Case QUOTE
sb.Append(""")
Case "'"
sb.Append("'")
Case "<"
sb.Append("<")
Case ">"
sb.Append(">")
Case "&"
sb.Append("&")
Case "ö"
sb.Append("ö")
Case "ä"
sb.Append("ä")
Case "ü"
sb.Append("ü")
Case "Ö"
sb.Append("Ö")
Case "Ä"
sb.Append("Ä")
Case "Ü"
sb.Append("Ü")
Case "ß"
sb.Append("ß")
Case Else
sb.Append(C)
End Select
Next
Return sb.ToString
End Sub`
' Escape characters for URL
' Can be used to sent emails using mailto:
`Public Sub EscapeURL(Raw As String) As String
Dim sb As StringBuilder
sb.Initialize
For i = 0 To Raw.Length - 1
Dim C As Char = Raw.CharAt(i)
Select C
Case CRLF
sb.Append("%0A")
'sb.Append("%0A%0D")
Case Chr(0)
sb.Append("%20")
Case Chr(32)
sb.Append("%20")
Case "!"
sb.Append("%21")
Case """"
sb.Append("%22")
Case "#"
sb.Append("%23")
Case "$"
sb.Append("%24")
Case "%"
sb.Append("%25")
Case "&"
sb.Append("%26")
Case "'"
sb.Append("%27")
Case "("
sb.Append("%28")
Case ")"
sb.Append("%29")
Case "*"
sb.Append("%2A")
Case "+"
sb.Append("%2B")
Case ","
sb.Append("%2C")
Case "/"
sb.Append("%2F")
Case ":"
sb.Append("%3A")
Case ";"
sb.Append("%3B")
Case "="
sb.Append("%3D")
Case "?"
sb.Append("%3F")
Case "@"
sb.Append("%40")
Case "["
sb.Append("%5B")
Case "]"
sb.Append("%5D")
Case "<"
sb.Append("%3C")
Case ">"
sb.Append("%3E")
Case "-"
sb.Append("%20")
' UMLAUTE
Case "Ä"
sb.Append("%C3%84")
Case "ä"
sb.Append("%C3%A4")
Case "Ö"
sb.Append("%C3%96")
Case "ö"
sb.Append("%C3%B6")
Case "Ü"
sb.Append("%C3%9C")
Case "ü"
sb.Append("%C3%BC")
Case "ß"
sb.Append("%C3%9F")
Case Else
sb.Append(C)
End Select
Next
Return sb.ToString
End Sub`

Changed: 2018-04-30 10:22, Category: Strings, File: T6C255M1846.htm, ID: 1846