2013-11-04 31 views
0

有沒有人有辦法將字符串轉換爲escape() ed版本來自JS?我會很高興有一個單一的命令或任何類型的功能。VB.net相當於JS中的換碼

+0

我覺得'HttpUtility'有所有這些東西.. –

+0

原始字符串的例子和期望的結果? –

+0

<插入長字符串> =!END!是我希望轉換成的字符串<插入長字符串>%3D%21END%21 – Lugia101101

回答

1

Buried in the linked answers

  1. 參考Microsoft.JScript.dll項目參考
  2. 使用Microsoft.JScript.GlobalObject.escape函數來進行編碼。

這是相同的過程,無論您使用VB.Net,C#還是其他.Net語言。他們必須全部支持string/String


另外,這裏是一些代碼做同樣的事情,它的反映和轉換。

Public Shared Function Escape(str As String) As String 
    Dim str2 As String = "ABCDEF" 
    Dim length As Integer = str.Length 
    Dim builder As New StringBuilder(length * 2) 
    Dim num3 As Integer = -1 

    While System.Threading.Interlocked.Increment(num3) < length 
     Dim ch As Char = str(num3) 
     Dim num2 As Integer = ch 
     If (((&H41 > num2) OrElse (num2 > 90)) AndAlso _ 
       ((&H61 > num2) OrElse (num2 > &H7a))) AndAlso _ 
       ((&H30 > num2) OrElse (num2 > &H39)) Then 
      Select Case ch 
       Case "@"C, "*"C, "_"C, "+"C, "-"C, "."C, "/"C 
        GoTo Label_0125 
      End Select 
      builder.Append("%"C) 
      If num2 < &H100 Then 
       builder.Append(str2(num2/&H10)) 
       ch = str2(num2 Mod &H10) 
      Else 
       builder.Append("u"C) 
       builder.Append(str2((num2 >> 12) Mod &H10)) 
       builder.Append(str2((num2 >> 8) Mod &H10)) 
       builder.Append(str2((num2 >> 4) Mod &H10)) 
       ch = str2(num2 Mod &H10) 
      End If 
     End If 

     Label_0125: 
     builder.Append(ch) 
    End While 

    Return builder.ToString() 
End Function