英文:
VB.net need help parsing a substring
问题
Dim someValue As String = Request.Params("search")
这是我的字符串的值:
?MyId1=VALUE1&MyId2=VALUE2&MyBoolen=True
我想要捕获VALUE2
。我尝试了下面的代码,但没有成功。
如果someValue不为空,则尝试以下代码:
Dim x = someValue.Substring(someValue.IndexOf("&"c) + 1)
If Not String.IsNullOrEmpty(x) Then
Dim y = x.Substring(x.IndexOf("="c) + 1)
End If
英文:
I have a value I am capturing from an Http Request
Dim someValue As String = Request.Params("search")
Here is the value of my string:
?MyId1=VALUE1&MyId2=VALUE2&MyBoolen=True
I am trying to capture VALUE2
. I tried the below code, but haven't had any success.
If Not String.IsNullOrEmpty(someValue) Then
Dim x = someValue.Substring(someValue.IndexOf("&"c) + 1)
If Not String.IsNullOrEmpty(someValue) Then
Dim y = x.Substring(someValue.IndexOf("="c) + 1)
End If
End If
How can I do this?
答案1
得分: 3
以下是翻译好的部分:
"这看起来像是你在过度思考。Request
对象将允许你直接查找 MyId2
的值:
Dim MyId2 As String = Request.QueryString("MyId2")
这也可能是一个嵌套的查询字符串,实际上你可能有类似这样的内容:
/?search=%3FMyId1%3DVALUE1%26MyId2%3DVALUE2%26MyBoolen%3DTrue
这将在运行时 URL 解码 search
元素后给你原始的字符串。在这种情况下,你应该查看 HttpUtility.ParseQueryString() 方法 而不是尝试自己处理:
Dim search As String = Request.Params("search")
Dim searchValues As NameValueCollection = HttpUtility.ParseQueryString(search)
Dim MyId2 As String = searchValues("MyId2")
甚至可以写成一行代码,如果我们真的想要:
Dim MyId2 As String = HttpUtility.ParseQueryString(Request.Params("search"))("MyId2")
但如果你真的想手动解析它,这其中的一个好处是一切都应该是 URL 编码的。这意味着你不必担心数据中的杂乱的 &
或 =
字符,一个简单的 Split()
调用应该是安全的:
Dim MyId2 As String = ""
Dim items = someValue.Substring(1).Split("&"c)
For Each item As String In Items
Dim parts = item.Split("="c)
If parts(0) = "MyId2" Then
MyId2 = parts(1)
Exit For
End If
Next
或者
Dim parts() As String = someValue.Substring(1).Split("&"c).
Select(Function(s) s.Split("="c)).
FirstOrDefault(Function(p) p(0) = "MyId2")
Dim MyId2 As String = If(parts IsNot Nothing, parts(1), "")
英文:
It looks like you're overthinking this. The Request
object will let you look up the MyId2
value directly:
Dim MyId2 As String = Request.QueryString("MyId2")
It's also possible this is a nested query string, where what you actually have is something more like this:
/?search=%3FMyId1%3DVALUE1%26MyId2%3DVALUE2%26MyBoolen%3DTrue
This would give you the original string after the runtime URL Decodes the search
element. In that case, you should look at the HttpUtility.ParseQueryString()
method, rather than trying to do this yourself:
Dim search As String = Request.Params("search")
Dim searchValues As NameValueCollection = HttpUtility.ParseQueryString(search)
Dim MyId2 As String = searchValues("MyId2")
Which could even be written as a one-liner if we really wanted:
Dim MyId2 As String = HttpUtility.ParseQueryString(Request.Params("search"))("MyId2")
But if you really wanted to parse this by hand, one of the nice things about this is everything should be URL-encoded. This means you don't have worry about stray &
or =
characters as part of the data, and a simple Split()
call should be safe:
Dim MyId2 As String = ""
Dim items = someValue.Substring(1).Split("&"c)
For Each item As String In Items
Dim parts = item.Split("="c)
If parts(0) = "MyId2" Then
MyId2 = parts(1)
Exit For
End If
Next
Or
Dim parts() As String = someValue.Substring(1).Split("&"c).
Select(Function(s) s.Split("="c)).
FirstOrDefault(Function(p) p(0) = "MyId2")
Dim MyId2 As String = If(parts IsNot Nothing, parts(1), "")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论