get a variable from the query string
-
if i have a string that contains a URL, ana i need to extract a variable from the query string, i made two solutions , which one should i use ??
Dim strUrl as String= "http://www.mySite.com?key=5&book=abc" dim strParam as String = "key" dim strResult as String Dim strKey As String = strUrl.ToLower.Substring(strUrl.IndexOf("?")) Dim strQuery As String() = strKey.Split("&") For i As Integer = 0 To strQuery.Length - 1 If strQuery(i).ToLower.IndexOf(strParam.ToLower) <> -1 Then strQuery = strQuery(i).Split("=") strKey = strQuery(i) strResult = strKey Exit For End If Next i 'or Dim strQuery As String = strURL.Substring(strURL.IndexOf("?") + 1) Dim objRequest As New HttpRequest("",strURL, strQuery) Dim strResult as String = objRequest.QueryString("key")
your opinions please
When you get mad...THINK twice that the only advice Tamimi - Code
-
if i have a string that contains a URL, ana i need to extract a variable from the query string, i made two solutions , which one should i use ??
Dim strUrl as String= "http://www.mySite.com?key=5&book=abc" dim strParam as String = "key" dim strResult as String Dim strKey As String = strUrl.ToLower.Substring(strUrl.IndexOf("?")) Dim strQuery As String() = strKey.Split("&") For i As Integer = 0 To strQuery.Length - 1 If strQuery(i).ToLower.IndexOf(strParam.ToLower) <> -1 Then strQuery = strQuery(i).Split("=") strKey = strQuery(i) strResult = strKey Exit For End If Next i 'or Dim strQuery As String = strURL.Substring(strURL.IndexOf("?") + 1) Dim objRequest As New HttpRequest("",strURL, strQuery) Dim strResult as String = objRequest.QueryString("key")
your opinions please
When you get mad...THINK twice that the only advice Tamimi - Code
Neither.
Dim url as String= "http://www.mySite.com?key=5&book=abc"
Dim key as String = "key"
Dim result as StringDim queryString As String = url.Substring(url.IndexOf("?"))
Dim params As String() = queryString.Split("&")
For i As Integer = 0 To params.Length - 1
Dim param As String() = params(i).Split("=")
If param(0).ToLower = key.ToLower Then
result = param(1)
Exit For
End If
Next i--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
-
Neither.
Dim url as String= "http://www.mySite.com?key=5&book=abc"
Dim key as String = "key"
Dim result as StringDim queryString As String = url.Substring(url.IndexOf("?"))
Dim params As String() = queryString.Split("&")
For i As Integer = 0 To params.Length - 1
Dim param As String() = params(i).Split("=")
If param(0).ToLower = key.ToLower Then
result = param(1)
Exit For
End If
Next i--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
Guffa wrote:
result = param(1)
i think i made a type mistake , strKey = strQuery(1) :) could you please tell what's wrong with the second solution ?? the one that used the HttpRequest
When you get mad...THINK twice that the only advice Tamimi - Code
-
Guffa wrote:
result = param(1)
i think i made a type mistake , strKey = strQuery(1) :) could you please tell what's wrong with the second solution ?? the one that used the HttpRequest
When you get mad...THINK twice that the only advice Tamimi - Code
Tamimi - Code wrote:
i think i made a type mistake , strKey = strQuery(1)
No, that's not a mistake. The array is named
param
, notstrQuery
. The result goes into theresult
variable, not some variable that isn't even declared.Tamimi - Code wrote:
could you please tell what's wrong with the second solution ?? the one that used the HttpRequest
The HttpRequest object is not supposed to be used that way. The documentation for the constructor says: "This constructor supports the .NET Framework infrastructure and is not intended to be used directly from your code."
--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
-
Tamimi - Code wrote:
i think i made a type mistake , strKey = strQuery(1)
No, that's not a mistake. The array is named
param
, notstrQuery
. The result goes into theresult
variable, not some variable that isn't even declared.Tamimi - Code wrote:
could you please tell what's wrong with the second solution ?? the one that used the HttpRequest
The HttpRequest object is not supposed to be used that way. The documentation for the constructor says: "This constructor supports the .NET Framework infrastructure and is not intended to be used directly from your code."
--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
Guffa wrote:
"This constructor supports the .NET Framework infrastructure and is not intended to be used directly from your code."
i read that on the documentation, but i just wonding way, it could save the looping time. thank you for your response
When you get mad...THINK twice that the only advice Tamimi - Code